I want to locate all image tags in my html with src not containing http:// and prepend http:// to the src attribute.
I have got the regex to find all img tags not starting with http://. I’m having some trouble appdening http:// to the src attribute alone. How can I achieve this using regex replace.
<img [^<]*src="(?!http://)(?<source>[^"]*)"[^<]*/>
Source will contain the src value. I just need it to say $2 = "http://" + $2. How can I write this in c# code.
Since you don’t want to break existing tags, you will need to assign groups to the parts of the string you are not interested in; in order to be able to include those parts of the match in the replace pattern:
Then the replace is trivial:
(Also, this might work for your application use case, but I should mention, that in general it is not considered a good idea to parse HTML with regex)