The following regex
var patt1=/[0-9a-z]+$/i;
extracts the file extension of strings such as
filename-jpg
filename#gif
fil
ONELINER:
let ext = (filename.match(/\.([^.]*?)(?=\?|#|$)/) || [])[1]
above solution include links. It takes everything between last dot and first "?" or "#" char or string end. To ignore "?" and "#" characters use /\.([^.]*)$/. To ignore only "#" use /\.([^.]*?)(?=\?|$)/. Examples
function getExtension(filename) {
return (filename.match(/\.([^.]*?)(?=\?|#|$)/) || [])[1];
}
// TEST
[
"abcd.Ef1",
"abcd.efg",
"abcd.efg?aaa&a?a=b#cb",
"abcd.efg#aaa__aa?bb",
"abcd",
"abcdefg?aaa&aa=bb",
"abcdefg#aaa__bb",
].forEach(t=> console.log(`${t.padEnd(21,' ')} -> ${getExtension(t)}`))