• C#正则删除HTML标签


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;

    public partial class Ceshi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string str = Regex.Replace("AAA\nBBB\nCCC<br>", "^", "开始=>", RegexOptions.Multiline | RegexOptions.IgnoreCase);//多行模式,每行前面加 '开始=>'
                Response.Write(str);
                string s = @"<html><title>title\\标题</title><head><script>alert('JS脚本');</script>head头部</head><body><table><tr><td><!--注释的东西-->TD的内容1</td><td>TD的内容2</td></table><div style='100px;'>DIV的内容</div><span>span内容1</spaN><spAN>span内容2</SPAN></body></html>";
                Response.Write(ClearHTMLTags(s));
            }
        }
        public static string ClearHTMLTags(string HTML)
        {
            string[] Regexs ={
                            @"<script[^>]*?>.*?</script>",
                            @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
                            @"([\r\n])[\s]+",
                            @"&(quot|#34);",
                            @"&(amp|#38);",
                            @"&(lt|#60);",
                            @"&(gt|#62);",
                            @"&(nbsp|#160);",
                            @"&(iexcl|#161);",
                            @"&(cent|#162);",
                            @"&(pound|#163);",
                            @"&(copy|#169);",
                            @"&#(\d+);",
                            @"-->",
                            @"<!--.*\n"
            };

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

            string s = HTML;
            for (int i = 0; i < Regexs.Length; i++)
            {
                s = new Regex(Regexs[i], RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(s, Replaces[i]);
            }
            s.Replace("<", "");
            s.Replace(">", "");
            s.Replace("\r\n", "");
            return s;
        }
    }

  • 相关阅读:
    SQLServer数据库中开启CDC导致“事务日志空间被占满,原因为REPLICATION”的原因分析和解决办法
    译:SQL Server的Missing index DMV的 bug可能会使你失去理智---慎重看待缺失索引DMV中的信息
    SQLServer中间接实现函数索引或者Hash索引
    MySQL缓存分类和配置
    MySQL系统变量配置基础
    MySQL索引统计信息更新相关的参数
    Sql Server优化---统计信息维护策略
    SQL Server 用角色(Role)管理数据库权限
    sp_executesql 或者 EXECUTE 执行动态sql的权限问题
    关于T-SQL重编译那点事,内联函数和表值函数在编译生成执行计划的区别
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2515278.html
Copyright © 2020-2023  润新知