Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  15] [ 5]  / answers: 1 / hits: 17844  / 15 Years ago, wed, august 19, 2009, 12:00:00

In JavaScript I have a string containing a DOM fragment. How would I find and replace the src attribute of an image?



I would like to replace the path of all images with a new path but keeping the image name. Not all the paths are the same and can come from various locations.
My regular expression skills are poor at best.



For example:



Change

<img src='path/to/image/name.jpg' />

into

<img src='newPath/name.jpg' />

More From » regex

 Answers
40

Try this:



str.replace(/src='(?:[^'/]*/)*([^']+)'/g, src='newPath/$1');

[#98881] Friday, August 14, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrencegreysons

Total Points: 674
Total Questions: 102
Total Answers: 105

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;