zoukankan      html  css  js  c++  java
  • 清理网页中的HTML

     public string ClearHtml(string text)//过滤html,js,css代码
        {
            text = text.Trim();
            if (string.IsNullOrEmpty(text))
                return string.Empty;
            text = Regex.Replace(text, "<head[^>]*>(?:.|[
    ])*?</head>", "");
            text = Regex.Replace(text, "<script[^>]*>(?:.|[
    ])*?</script>", "");
            text = Regex.Replace(text, "<style[^>]*>(?:.|[
    ])*?</style>", "");
            
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\n)*?>)", ""); //<br> 
            text = Regex.Replace(text, "\&[a-zA-Z]{1,10};", "");
            text = Regex.Replace(text, "<[^>]*>", "");
    
            text = Regex.Replace(text, "(\s*&[n|N][b|B][s|S][p|P];\s*)+", ""); // 
            text = Regex.Replace(text, "<(.|\n)*?>", string.Empty); //其它任何标记
            text = Regex.Replace(text, "[\s]{2,}", " "); //两个或多个空格替换为一个
    
            text = text.Replace("'", "''");
            text = text.Replace("
    ", "");
            text = text.Replace("  ", "");
            text = text.Replace("	", "");
            return text.Trim();
        }
    

      

  • 相关阅读:
    Linux命令(一)
    数据库SQL学习(一)
    Eclipse
    VsCode支持的markdown语法参考(一)
    常用算法Tricks(一)
    dispose方法的使用
    收藏一个链接
    我还不知道取什么名字
    NioSocket的用法
    随便乱塞塞2~
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/4796687.html
Copyright © 2011-2022 走看看