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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解C#实现过滤html标签并保留a标签的方法的相关内容吗,在本文为您仔细讲解C#实现过滤html标签并保留a标签的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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#程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们