源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#实现过滤html标签并保留a标签的方法

  • 时间:2020-02-02 18:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#实现过滤html标签并保留a标签的方法
本文实例展示C#实现过滤html标签,汉字间空格,制表符,并保留a标签的方法。分享给大家供大家参考之用。具体方法如下: 可以在公共类如Common中定义如下方法:
  public static string ClearHtmlExceptA(string html) {
    string acceptable = "a";
    string stringPattern = @"</?(?(?=" + acceptable + @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*?\1?)?)*\s*/?>";
    html = Regex.Replace(html, stringPattern, "");
    html = Regex.Replace(html, @"[\t\n]", "", RegexOptions.IgnoreCase);
    html = Regex.Replace(html, @"[\r]", "", RegexOptions.IgnoreCase);
    //html = Regex.Replace(html, @"[\t\n\r\s]","",RegexOptions.IgnoreCase);
    return html;
  }

然后在你需要过滤的字段添加这个方法即可实现过滤功能。希望本文所述对大家的C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部