C# 过滤html标签,保留a标签的做法
C# 过滤html标签,汉字间空格,制表符,保留a标签的方法
操作方法
- 01
在公共类如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; } 然后在你需要过滤的字段添加这个方法即可
赞 (0)