求一则过滤hmtl标签的正则表达式

lmjob 2010-03-10 04:21:54
在发布的新闻内容中存在很多<table>……</table><div>……</div>等标签
但现在除了<font></font><p></p><img></img> 这些标签不过滤之外,
所有<></>都过滤掉,求该正则表达式,感谢!!
...全文
46 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2010-03-10
  • 打赏
  • 举报
回复
最好有源字串 和你要结果
lmjob 2010-03-10
  • 打赏
  • 举报
回复
关注的人,难道分给少了?
albwangyang 2010-03-10
  • 打赏
  • 举报
回复
public static string gethtml(string html)
{
html = System.Web.HttpContext.Current.Server.HtmlDecode(html);
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</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>) 属性 网管网bitsCN.com
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>", "");
html = html.Replace("</tbody>", "");
html = html.Replace("<tbody>", "");
html = html.Replace("</table>", "");
html = html.Replace("<table>", "");
html = html.Replace("</tr>", "");
html = html.Replace("<tr>", "");
html = html.Replace("</td>", "");
html = html.Replace("<td>", "");
System.Text.RegularExpressions.Regex regular = new System.Text.RegularExpressions.Regex("<(S*?)[^>]*>");
System.Text.RegularExpressions.MatchCollection replaceStr = regular.Matches(html);
foreach (System.Text.RegularExpressions.Match match in replaceStr)
{
html = html.Replace(match.ToString(), "");
}
html = html.IndexOf('<') > 0 ? html.Substring(0, html.IndexOf('<')) : html;
return html;
}

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧