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) { let a; if ((a = file.match(/\.([^\.]+)$/)) == null) return ''; return a[1]; } function exampleUse() { const a = "hello_world.txt"; const ext = getExtension(a); alert('extension of ' + a + ' = ' + ext); }