Using C# regular expressions to remove HTML tags

前端 未结 10 1690
悲&欢浪女
悲&欢浪女 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:52

    Use this method to remove tags:

    public string From_To(string text, string from, string to)
    {
        if (text == null)
            return null;
        string pattern = @"" + from + ".*?" + to;
        Regex rx = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        MatchCollection matches = rx.Matches(text);
        return matches.Count <= 0 ? text : matches.Cast().Where(match => !string.IsNullOrEmpty(match.Value)).Aggregate(text, (current, match) => current.Replace(match.Value, ""));
    }
    

提交回复
热议问题