Reputation: 8171
I have the following two scenarios, where i need to replace either a href=".."
value or src=".."
value.
The pattern is
<img src="/~/media/75F8BA07F3BC4C3F91A71D6A049E6BD4.ashx" alt="" />
<a href="/~/link.aspx?_id=2CD5F3FBD0334A7DA7CB81F9520BEED5&_z=z">Some text</a>
The GUID is always 32 charaters long, and when found, I need to replace the entire href og src tag, of the element, with a new value.
Any ideas as to how this can be done?
Upvotes: 0
Views: 57
Reputation: 34180
This is the pattern you need:
(src="(\/~\/media\/([A-Z0-9]{32})\.ashx)")|(href="(\/~\/link\.aspx\?_id=([A-Z0-9]{32})&_z=z"))
Have a look at DEMO
Upvotes: 1
Reputation: 522254
Here is code for replacing the image tags as you described:
string input = "<img src="/~/media/75F8BA07F3BC4C3F91A71D6A049E6BD4.ashx" alt="" />";
input = Regex.Replace(input,
@"<img [^>]*src=""/~/([^/]+)/[A-Z0-9]{32}[^""]*""[^>]*/>",
"<img src=\"/$1/myfolder/myimage.png\" alt=\"\" />");
I would do a separate replacement on the anchor tags, and I will leave this to you as a follow up exercise.
By the way, it is in general bad practice to manipulate HTML using regular expressions, so you might want to move away from this if possible.
Upvotes: 0