Replacing specific image with a new image based on file name

Hi,

I have the following script which pulls an image source name and then outputs just the file name.

What I now want to do in addition to the below is to replace specific image names with another, for example, if the image name is IMAGE1.jpg, I want to replace it with IMAGE20.jpg and also dropping the .jpf file extension.

What would be the best way to do this based on the below?


$(".picture img").each(function () {
    var caption = $(this).attr('src').replace(/\\.jpg/, '').split('/').pop();
    $(this).parent().next().text(caption);
});

Thanks

Hi,

Maybe:

$(".picture img").each(function () { 
    var fileName = $(this).attr('src').replace(/\\.jpg/, '').split('/').pop();
    fileName = fileName.replace("IMAGE1", "IMAGE20");
}); 

Although this seems a bit pointless to me, as I’m sure that IMAGE1 isn’t the only name you’re trying to replace.

Perhaps you could say a little more about what you’re trying to do in more generic terms.