tags:

views:

6761

answers:

10

See code:

var file1 ="50.xsl";
var file2 =30.doc";"
getFileExtension(file1); //returs xsl
getFileExtension(file2); //returs doc

function getFileExtension(filename) {
/*TODO*/
}
+1  A: 
return filename.replace(/\.([a-zA-Z0-9]+)$/, "$1");

edit: Strangely (or maybe it's not) the $1 in the second argument of the replace method doesn't seem to work... Sorry.

p4bl0
It works perfect, but you missed out that you'll have to remove all the other content of the string: return filename.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1");
roenving
A: 

var arr=file1.split(".") return arr[1]

Mote
Not good if the filename is "file.name.ext" (a perfectly valid filename)
Oli
Why do you split on comma?
PhiLho
Another good point =)
Oli
+6  A: 
return /[^.]+$/.exec(filename);

Should do it.

Edit: In response to PhiLho's comment, use something like:

return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
Tom
Returns the filename if it has no extension...
PhiLho
Isn't it expensive to exec the regex twice?
Andrew Hedges
is it better to use regex than the lastindexof instead? thanks.
melaos
+1  A: 
var parts = filename.split('.');
return parts[parts.length-1];
yuku
+3  A: 
function getFileExtension(filename)
{
  var ext = /^.+\.([^.]+)$/.exec(filename);
  return ext == null ? "" : ext[1];
}

Tested with "a.b" (=> "b"), "a" (=> ""), ".hidden" (=> ""), "" (=> ""), null (=> "")
Also "a.b.c.d" (=> "d"), ".a.b" (=> "b"), "a..b" (=> "b").

PhiLho
+1  A: 
function file_get_ext(filename)
    {
    return typeof filename != "undefined" ? filename.substring(filename.lastIndexOf(".")+1, filename.length).toLowerCase() : false;
    }
Joe Scylla
this code works well
Fero
A: 

I just realized that it's not enough to put a comment on p4bl0's answer, though Tom's answer clearly solves the problem:

return filename.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1");
roenving
A: 

function extension(fname) {

var pos = fname.lastIndexOf(".");

var strlen = fname.length;

if(pos != -1 && strlen != pos+1){

var ext = fname.split(".");

var len = ext.length;

var extension = ext[len-1].toLowerCase();

}else{

extension = "No extension found";

}

return extension;

}

//usage

extension('file.jpeg')

always returns the extension lower cas so you can check it on field change works for:

file.JpEg

file (no extension)

file. (noextension)

A: 

function func() { var val=document.frm.filename.value; var arr=val.split("."); alert( arr[arr.length-1] ); var arr1=val.split("\"); alert(arr1[arr1.length-2]); if(arr[1]=="gif" || arr[1]=="bmp" || arr[1]=="jpeg") { alert("this is an image file "); } else { alert("this is not an image file"); } }

+7  A: 
return filename.split('.').pop();

Keep it simple :)

wallacer
I can't comment on the performance, but this one certainly looks clean! I'm using it. +1
pc1oad1etter
but in this case the file name looks like filname.tes.test.jpg. Kindly consider the output. I hope it will be false.
Fero
in that case the output is "jpg"
wallacer