Dave's Brain

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); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.