过滤掉字符串中的锚标记

Filtering out anchor tags in a string

提问人:Aaron Sanders 提问时间:8/25/2008 最后编辑:Jeff AtwoodAaron Sanders 更新时间:3/3/2013 访问量:1230

问:

我需要过滤掉字符串中的锚标记。例如

Check out this site: <a href="http://www.stackoverflow.com">stackoverflow</a>

我需要能够过滤掉锚标记:

Check out this site: http://www.stackoverflow.com

这种格式也可能不是恒定的。锚标记可能还有其他属性。此外,字符串中可能有多个锚标记。在它进入数据库之前,我正在 vb.net 进行过滤。

asp.net html vb.net

评论


答:

8赞 Rich Reuter 8/25/2008 #1

这是一个应该有效的简单正则表达式。

Imports System.Text.RegularExpressions

' ....

Dim reg As New Regex("<a.*?href=(?:'|"")(.+?)(?:'|"").*?>.+?</a>")
Dim input As String = "This is a link: <a href='http://www.stackoverflow.com'>Stackoverflow</a>"
input = reg.Replace(input, "$1", RegexOptions.IgnoreCase)