So a Regex pro I am not, and I’m looking for a good way to do this. I have a large string which contains a variable number <img> tags. I need to change the path on all of these images to images/. The large string also contains other stuff not just these img’s.
<img src='http://server.com/stuff1/img1.jpg' />
<img src='http://server.com/stuff2/img2.png' />
Replacing the server name with a ReplaceAll() I could do, it’s the variable path in the middle I’m clueless on how to include. It doesn’t necessarily need to be a regex, but looping through the entire string just seems wasteful.
The following should work for replacing all
<img>tags that link to a jpg from server.com:If you are using double quotes around the
srcproperty:This works because
[^']*/will match as many characters that are not single quotes as possible, and then a literal ‘/’, so it will consume the entire path. If all<img>tags are from server.com you can removehttp://server.comand the regex will work the same way.Note that you can shorten this even more if you know in advance that all of the image tags need to be replaced, or if your string only consists of
<img>tags, for example you could use the following to just replace the path in allsrcproperties:Just add more of the literal strings you want to match to the regex if this replaces more than you want.