Filtering out anchor tags in a string
Question
I need to filter out anchor tags in a string. For instance,
Check out this site: <a href="http://www.stackoverflow.com">stackoverflow</a>
I need to be able to filter out the anchor tag to this:
Check out this site: http://www.stackoverflow.com
That format may not be constant, either. There could be other attributes to the anchor tag. Also, there could be more than 1 anchor tag in the string. I'm doing the filtering in vb.net before it goes to the database.
Solution
Here's a simple regular expression that should work.
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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow