Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering out anchor tags in a string

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.

like image 580
Aaron Sanders Avatar asked Aug 25 '08 01:08

Aaron Sanders


People also ask

Is it possible to extract text from an HTML anchor tag?

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.

How to strip out tags from an HTML page?

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.

How to get the anchor properties of a Div in JavaScript?

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.


1 Answers

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)
like image 60
Rich Reuter Avatar answered Sep 20 '22 00:09

Rich Reuter