日期:2014-05-17 浏览次数:20528 次
string test = @"<div id=""0"" >
0
</div>
<div id=""1"" class=""a"">
1
<div id=""2"">
2
</div>
</div>";
//根据class=a来匹配
Regex reg = new Regex(@"(?isx)<div[^>]*class=(['""]?)a\1[^>]*?>(?><div[^>]*> (?<Open>)|</div> (?<-Open>)|(?:(?!</?div\b).)*)*(?(Open)(?!))</div>");
string match_str = reg.Match(test).Value;
/*
<div id=""1"" class=""a"">
1
<div id=""2"">
2
</div>
</div>
*/
------解决方案--------------------
坐等大婶,试问一下一定要用正则表达式吗?jquery不行吗?
------解决方案--------------------
using System.Text.RegularExpressions; //需要引用
public string HtmlFilter(string strHtml) //从字符串里删除HTML
{
Regex regex = new Regex("<[^>]*>", RegexOptions.IgnoreCase);
string strOutput = regex.Replace(strHtml, "");
return strOutput;
}