Reputation: 612
I have a full HTML string. It looks like this:
<html>
<head>
</head>
<body>
This is a test
<img width=403 height="302" id="someid1" src="http://mysite.com/images1">
<img width="456" height=300 src="http://mysite.com/images2" id="someid2">
</body>
</head>
What I would like to do is clean up the source. I want to remove all widths and heights in the img tags only. I want to preserve the ID and SRC attributes.
Upvotes: 1
Views: 1036
Reputation: 35353
You don't need regex here. Using an html parser like HtmlAgilityPack would be better..
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
foreach (var img in doc.DocumentNode.Descendants("img"))
{
img.Attributes.Remove("width");
img.Attributes.Remove("height");
}
var newhtml = doc.DocumentNode.OuterHtml;
Upvotes: 3