c# 正则html标签剔除方法的实现代码

发布时间:2020-04-22编辑:脚本学堂
c#代码实现的正则html标签剔除的方法,有需要的朋友,可以参考下。

方法1:
public string checkStr(string html)
 {
     System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[sS]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[sS]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"<img[^>]+>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
     html = regex1.Replace(html, ""); //过滤<script></script>标记
     html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
     html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
     html = regex4.Replace(html, ""); //过滤iframe
     html = regex5.Replace(html, ""); //过滤frameset
     html = regex6.Replace(html, ""); //过滤frameset
     html = regex7.Replace(html, ""); //过滤frameset
     html = regex8.Replace(html, ""); //过滤frameset
     html = regex9.Replace(html, "");
     html = html.Replace(" ", "");
     html = html.Replace("</strong>", "");
     html = html.Replace("<strong>", "");
     return html;
}

方法2:
 

复制代码 代码示例:

#region 过滤掉 html代码
public static string StripHTML(string strHtml)
{
string [] aryReg ={
@"<script[^>]*?>.*?</script>",

@"<(/s*)?!?((w+:)?w+)(w+(s*=?s*(([""'])([""'tbnr]|[^7])*?7|w+)|.{0})|s)*?(/s*)?>",
@"([rn])[s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(d+);",
@"-->",
@"<!--.*n"
};

string [] aryRep = {
"",
"",
"",
""",
"&",
"<",
">",
" ",
"xa1",//chr(161),
"xa2",//chr(162),
"xa3",//chr(163),
"xa9",//chr(169),
"",
"rn",
""
};

string newReg =aryReg[0];
string strOutput=strHtml;
for(int i = 0;i<aryReg.Length;i++)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i],System.Text.RegularExpressions.RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput,aryRep[i]);
}
strOutput.Replace("<","");
strOutput.Replace(">","");
strOutput.Replace("rn","");
return strOutput;
}
#endregion