zoukankan      html  css  js  c++  java
  • 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;
        }
    }

  • 相关阅读:
    开发人员需要熟知的常用Linux命令之四:Scp Leone
    linux下WordPress文件夹权限设置 Leone
    计算机经典教材(计算机牛人的必由之路)
    wampserver打开localhost显示 已经找到网站,正在等待回应。
    ffmpeg常用数据结构4
    ffmpeg常用数据结构3
    ffmpeg程序流程图
    ffmpeg常用数据结构2
    ffmpeg常用数据结构一
    热门wordpress插件 TOP 10
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2515278.html
Copyright © 2011-2022 走看看