zoukankan      html  css  js  c++  java
  • 过滤字符串中的html标签

    C#中,我们有时需要过滤掉字符串中的部分html标签,以下是一些简单的html标签过滤方法,使用的主要方式是正则表达式

    public static string ClearHtml(string html)
            {
               if(string.IsNullOrEmpty(html))
                {
                    return "";
                }
                //去除a标签
                html = Regex.Replace(html, @"<as*[^>]*>", "", RegexOptions.IgnoreCase);
                html = Regex.Replace(html, @"</a>", "", RegexOptions.IgnoreCase);
                //去除span标签
                html = Regex.Replace(html, @"<spans*[^>]*>", "", RegexOptions.IgnoreCase);
                html = Regex.Replace(html, @"</span>", "", RegexOptions.IgnoreCase);
                //去除所有的样式
                html = Regex.Replace(html, @"sstyle=""([^"";]+;?)+""", "", RegexOptions.IgnoreCase);
    
                //去除所有的html标签
                html = Regex.Replace(html, @"<[^>]*>|&nbsp;", "", RegexOptions.IgnoreCase);
                return html;
            }
  • 相关阅读:
    FLASH开发[00]
    slickedit使用小技巧
    网络编程[34]
    网络编程[39]
    网络编程[41]
    网络编程[37]
    网络编程[32]
    网络编程[35]
    Apache安装和配置
    网络编程[40]
  • 原文地址:https://www.cnblogs.com/getpower/p/5788557.html
Copyright © 2011-2022 走看看