Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search string 2

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
US
I can do this using PHP but not sure on syntax for javascript.. I have a variable: yo = test.jpg. I need to get all the characters after the period. In other words, I just need to get the file extension (excluding the period). Any thoughs would be great!
 
You could use the split() method to get an array.

Code:
var yo = "test.jpg";
var fileparts = yo.split(".");
var ext = "." + fileparts[1];

or you could use substring/indexof

Code:
var yo = "test.jpg";
var ext = yo.substring(yo.indexOf("."), yo.length);

Of course, this would only work when there is only one "." in the file name.

Ron Wheeler
 
A slight variation on ninjadeathmonkey's code to handle the possibility of more than one period in the name:
Code:
var yo = "test.jpg";
var fileparts = yo.split(".");
var ext = "." + fileparts[fileparts.length - 1];
and
Code:
var yo = "test.jpg";
var ext = yo.substring(yo.lastIndexOf("."));

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top