The following regex
var patt1=/[0-9a-z]+$/i;
extracts the file extension of strings such as
filename-jpg
filename#gif
filename.png
How to modify this regular expression to only return an extension when string really is a filename with one dot as separator ? (Obviously filename#gif is not a regular filename)
UPDATE Based on tvanofsson's comments I would like to clarify that when the JS function receives the string, the string will already contain a filename without spaces without the dots and other special characters (it will actually be handled a slug
). The problem was not in parsing filenames but in incorrectly parsing slugs - the function was returning an extension of jpg when it was given filename-jpg when it should really return null
or empty string and it is this behaviour that needed to be corrected.