How can I insert a string before the extension in an image filename? For example, I need to convert this:
../Course/Assess/Responsive_Course_1_1.png
to this:
../Course/Assess/Responsive_Course_1_1_large.png
How can I insert a string before the extension in an image filename? For example, I need to convert this:
../Course/Assess/Responsive_Course_1_1.png
to this:
../Course/Assess/Responsive_Course_1_1_large.png
If you are not sure what could be the incoming file's extension then this helps:
If we assume that an extension is any series of letters, numbers, underscore or dash after the last dot in the file name, then:
Use javascript lastIndexOf, something like:
None of the answers works if file doesn't have extension. Here's a solution that works for all cases.
for files without extension and files includes extension. thanks @paul !
filename = filename.replace(/^([^.]+)$|(\.[^.]+)$/i, '$1' + "-thumb" + '$2');
Either $1 match a filename with no extension or $2 match an extension.