且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 C# 正则表达式删除 HTML 标签

更新时间:2023-02-18 09:02:30

如前所述,您不应该使用正则表达式来处理 XML 或 HTML 文档.它们在 HTML 和 XML 文档中表现不佳,因为无法以通用方式表达嵌套结构.

As often stated before, you should not use regular expressions to process XML or HTML documents. They do not perform very well with HTML and XML documents, because there is no way to express nested structures in a general way.

您可以使用以下内容.

String result = Regex.Replace(htmlDocument, @"<[^>]*>", String.Empty);

这适用于大多数情况,但在某些情况下(例如包含尖括号的 CDATA),这不会按预期工作.

This will work for most cases, but there will be cases (for example CDATA containing angle brackets) where this will not work as expected.