Using C# regular expressions to remove HTML tags

前端 未结 10 1901
悲&欢浪女
悲&欢浪女 2020-11-22 05:59

How do I use C# regular expression to replace/remove all HTML tags, including the angle brackets? Can someone please help me with the code?

10条回答
  •  爱一瞬间的悲伤
    2020-11-22 06:50

    try regular expression method at this URL: http://www.dotnetperls.com/remove-html-tags

    /// 
    /// Remove HTML from string with Regex.
    /// 
    public static string StripTagsRegex(string source)
    {
    return Regex.Replace(source, "<.*?>", string.Empty);
    }
    
    /// 
    /// Compiled regular expression for performance.
    /// 
    static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
    
    /// 
    /// Remove HTML from string with compiled Regex.
    /// 
    public static string StripTagsRegexCompiled(string source)
    {
    return _htmlRegex.Replace(source, string.Empty);
    }
    

提交回复
热议问题