Search code examples
javascriptregexreplacesrc

How To change a part of src attribute unknown


I'm using a javascript code that find an img element and preview it after replacing a part of its src attribute.

This part is created by user and i can't determine it.

Here is the code:

<img src="' + i[0].src.replace("/s**/", "/s726/") + '" />

** represents the unknown numbers ranged from 0 to 1600.
I'm looking for a regex or another way that dynamically gets this value and changes it to 726

Thank you

#Update Solution:

<img src="' + i[0].src.replace(/\/s[0-9]+(-*c*)\//, "/s726/") + '" />

Solution

  • You could try something like

    i[0].src.replace(/s([01]?[0-5]?[0-9]?[0-9]|1600)[^0-9]/, "s726")