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
It's called the basename. In fact, there's a unix/linux command for it: basename - strip directory and suffix from filenames.
A filename may (depending on the file system) include: name – base name of the file. extension (format or extension) – indicates the content of the file (e.g. . txt , .exe , .
If we assume that an extension is any series of letters, numbers, underscore or dash after the last dot in the file name, then:
filename = filename.replace(/(\.[\w\d_-]+)$/i, '_large$1');
None of the answers works if file doesn't have extension. Here's a solution that works for all cases.
function appendToFilename(filename, string){ var dotIndex = filename.lastIndexOf("."); if (dotIndex == -1) return filename + string; else return filename.substring(0, dotIndex) + string + filename.substring(dotIndex); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With