本文實例展示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#程序設計有所幫助。
新聞熱點
疑難解答