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.
Again, there are probably better ways to do this -- including DOM-related approaches I don’t know yet -- but if you need a JavaScript function to extract the text from an HTML anchor tag, I hope this is a helpful start.
In order to strip out tags we can use replace () function and can also use .textContent property, .innerText property from HTML DOM. HTML tags are of two types opening tag and closing tag. Opening tag: It starts with a ‘ < ‘, followed by an HTML keyword and ends with a ‘ > ‘. <html>, <br>, <title> are some examples of HTML opening tags.
to create a div, set the innerHTML of the div to the str` string, and then get the values by selecting the anchors. Then we set div.innerHTML to str to populate the div with the anchors. Next, we select the anchors with div.querySelectorAll. And then we spread the anchors into an array and call map to return the properties we’re looking for.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With