Dave
Dave

Reputation: 334

Regex add text after each match in a string javascript

I have string containing different size images see below:

let newstr=‘assets/images/iphoneMoviesImg.png 1x,assets/images/[email protected] 2x,assets/images/[email protected] 3x’

What I can to do is prevent the images from caching in javascript.

I am trying to build a regex expression that can match to the image extension mime type and add a unique number after to prevent caching. Below is a example of the result that I am after:

let newstr=‘assets/images/iphoneMoviesImg.png?3 1x,assets/images/[email protected]?3 2x,assets/images/[email protected]?3 3x’

Any advice would be much appreciated!

Upvotes: 0

Views: 697

Answers (1)

ProEvilz
ProEvilz

Reputation: 5455

You could use regex with replace() for this?

UPDATE OP wants to replace multiple mime types.

let newstr='assets/images/iphoneMoviesImg.gif 1x,assets/images/[email protected] 2x,assets/images/[email protected] 3x';

var res = newstr.replace(/(png)/g, 'png?3');
 res = res.replace(/(jpg)/g, 'jpg?3');
 res = res.replace(/(gif)/g, 'gif?3');

console.log(res);

Upvotes: 1

Related Questions