如题,什么标签不确定,什么属性不确定,属性值也不确定
比如,我要把class为"class2"的div标签替换成空字符串,这个正则该怎么写?
我现在有一个方法
public void RemoveElement(string html,string tagName,string attributeName,string attributeValue)
{
}
我想用正则来做,但是正则不知道该怎么写,请大家帮帮忙,万分感谢...
比如,我要把class为"class2"的div标签替换成空字符串,这个正则该怎么写?
我现在有一个方法
public void RemoveElement(string html,string tagName,string attributeName,string attributeValue)
{
}
我想用正则来做,但是正则不知道该怎么写,请大家帮帮忙,万分感谢...
using System.Text.RegularExpressions;Regex re = new Regex(@"<div[^>]*class=""class2""[^>]*>[\s\S]*?</div>", RegexOptions.Multiline);
string oldstr = "your string";
string newstr = re.Replace(oldstr, "replace to");
{
string reg = string.Format(@"(?is)<{0}[^>]*?{1}=(['""\s]?){2}\1[^>]*?>(((?!</?{0}).)*</{0}>)?", tag, attrName, attrValue);
return Regex.Replace(original, reg, "");
} string original = "aaaaa<div class=\"class2\" id=\"1\"/>bbbbb</div><font class=\"class2\" color=\"red\">cccc</font>";
Response.Write(RemoveTag(original, "div", "class", "class2"));
//结果:aaaaa<font class="class2" color="red">cccc</font>