0

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!

1
  • Do you only want regex solutions? Commented Sep 26, 2017 at 14:25

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @ProEvilz actually can would it be possible to also add in matches for .gif .jpg .png ?
@Dave Repeat the replace and swap png for which ever mime you need. Or you can match all in one go with (png|jpg|gif) but I'm not sure about how to map the correct mimetype with the match. If you use (png|jpg|gif) with the code above then all matches will become replaced with png?3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.