Insert a string before the extension in a filename

2020-05-25 01:29发布

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

7条回答
家丑人穷心不美
2楼-- · 2020-05-25 02:31
var s = '../Course/Assess/Responsive_Course_1_1.png'
s.replace(/\.png$/, '_large.png');

This will do the job. By the way, it's night here. :)

UPDATE:

A more general way would be this:

var s = '../Course/Assess/Responsive_Course_1_1.png';
s.replace(/(\.[^\.]+)$/, '_large$1');
查看更多
登录 后发表回答