该方法亲测可行,下面直接粘贴代码.

public string RemoveHTMLTags(string htmlStream)
{
if (htmlStream == null)
{
throw new Exception("Your input html stream is null!");
return null;
}
/*
* 最好把所有的特殊HTML标记都找出来,然后把与其相对应的Unicode字符一起影射到Hash表内,最后一起都替换掉
*/
//先单独测试,成功后,再把所有模式合并
//注:这两个必须单独处理
//去掉嵌套了HTML标记的JavaScript:(<script)[\\s\\S]*(</script>)
//去掉css标记:(<style)[\\s\\S]*(</style>)
//去掉css标记:\\..*\\{[\\s\\S]*\\}
htmlStream = Regex.Replace(htmlStream, "(<script)[\\s\\S]*?(</script>)|(<style)[\\s\\S]*?(</style>)", " ", RegexOptions.IgnoreCase);
//htmlStream = RemoveTag(htmlStream, "script");
//htmlStream = RemoveTag(htmlStream, "style");
//去掉普通HTML标记:<[^>]+>
//替换空格:&nbsp;|&amp;|&shy;| |­
htmlStream = Regex.Replace(htmlStream, "<[^>]+>|&nbsp;|&amp;|&shy;| |­|&bull;|&lt;|&gt;", " ", RegexOptions.IgnoreCase);
//htmlStream = RemoveTag(htmlStream);
//替换左尖括号
//htmlStream = Regex.Replace(htmlStream, "&lt;", "<");
//替换右尖括号
//htmlStream = Regex.Replace(htmlStream, "&gt;", ">");
//替换空行
//htmlStream = Regex.Replace(htmlStream, "[\n|\r|\t]", " ");//[\n|\r][\t*| *]*[\n|\r]
htmlStream = Regex.Replace(htmlStream, "(\r\n[\r|\n|\t| ]*\r\n)|(\n[\r|\n|\t| ]*\n)", "\r\n");
htmlStream = Regex.Replace(htmlStream, "[\t| ]{1,}", " ");
return htmlStream.Trim();
}

最新文章

  1. Linux学习心得之 Linux下ant安装与使用
  2. CI框架如何在主目录application目录之外使用uploadify上传插件和bootstrap前端框架:
  3. Resolving SQL Server Disk IO bottlenecks
  4. 关于AFNetworking菊花转圈圈的问题
  5. XAML: x:DeferLoadStrategy, x:Null
  6. [转]关于安装hadoop中出现的的 $HADOOP_HOME is deprecated 的解决方法
  7. 窗体移动API
  8. 使用Beanstalkd实现队列
  9. HDU 1247 Hat’s Words
  10. spring读取properties文件
  11. embedded dylibs/frameworks are only supported on iOS 8.0 and later 错误解决
  12. IPC之消息队列详解与使用
  13. Asp.Net 网站访问人数及在线人数
  14. 【LeetCode】89. Gray Code
  15. [Sdoi2017]相关分析 [线段树]
  16. MySQL InnoDB 修改表列Online DDL
  17. selenium-webdriver的二次封装(十)
  18. [模板] BSGS/扩展BSGS
  19. python控制流
  20. day0320 时间模块 collection模块

热门文章

  1. Javascript定时器学习笔记
  2. ASP.NET集成模式下的管道事件
  3. Oracle动态执行语句
  4. Java-继承 共3题
  5. MVVM架构~knockoutjs系列之级联select
  6. [Spring框架]Spring JDBCTmplate基础入门总结.
  7. ASP.net的文件扩展名
  8. Atitit 常用比较复杂的图像滤镜 attilax大总结
  9. Atitit apache 和guava的反射工具
  10. 在SSIS中的不同组件间使用局部临时表