Daniel Daniel - 1 month ago 8
Javascript Question

how to add more text to a string before a special char

I would like to modify a string using jQuery as following:

Existing value:

myimage_one.png


New value:
myimage_one_notold.png


How can I do it? Maybe with the
concat()
function?

Answer

You can use the replace() function on your string. In this case you could replace the .png with _notold.png:

var foo = 'myimage_one.png';
var bar = foo.replace('.png', '_notold.png');
console.log(bar);

Also note that this is a native JS method and has nothing to do with jQuery