具体实现如下:
//除掉HTML保留格式外的其他格式
string[] HoldTags = { "strong", "em", "u", "strike", "sub", "sup", "img", "b", "i", "div", "p", "center" };//保留的关键字
string RegStr = string.Format(@"<(?!((/?\s?{0})))[^>]+>", string.Join(@"\b)|(/?\s?", HoldTags));
System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex(RegStr, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string QuestionAllContent="<strong>中国</strong>真的好强大<tes>!</tes>";//要进行去掉的字符串
QuestionAllContent = Reg.Replace(QuestionAllContent, "");
最后QuestionAllContent的内容为:“="<strong>中国</strong>真的好强大!”
本文介绍了一种使用C#编程语言去除HTML标签的方法,通过正则表达式精确匹配并保留特定HTML标签,如strong、em等,同时移除其他非保留标签,确保文本内容的准确呈现。
270

被折叠的 条评论
为什么被折叠?



