Browse - Programming Tips - How can I get the extension of a file using javaScript?

Date: 2009jun20
Language: javaScript
Keywords: extension, suffix

Q.  How can I get the extension of a file using javaScript?

A.  Use this function

function getExtension(file) { var a; if ((a = file.match(/\.([^\.]+)$/)) == null) return ''; return a[1]; } function exampleUse() { var a, ext; a = "hello_world.txt"; ext = getExtension(a); alert('extension of ' + a + ' = ' + ext); }