2013-08-06 5 views
1

전체 HTML 문자열이 있습니다. 다음과 같이 보입니다 :전체 html 문자열의 img 태그 정리

<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> 

소스 코드를 정리하고 싶습니다. img 태그에서만 모든 너비와 높이를 제거하고 싶습니다. ID 및 SRC 속성을 유지하려고합니다.

+0

텍스트를 편집 할 때 무엇이 ​​문제입니까? –

답변

3

정규식이 필요하지 않습니다. HtmlAgilityPack과 같은 html 파서를 사용하면 더 좋을 것입니다.

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;