Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in Javascript to remove links

I have a string in JavaScript and it includes an a tag with an href. I want to remove all links and the text. I know how to just remove the link and leave the inner text but I want to remove the link completely.

For example:

var s = "check this out <a href='http://www.google.com'>Click me</a>. cool, huh?";

I would like to use a regex so I'm left with:

s = "check this out. cool, huh?";
like image 758
GivP Avatar asked Jun 06 '09 17:06

GivP


3 Answers

This will strip out everything between <a and /a>:

mystr = "check this out <a href='http://www.google.com'>Click me</a>. cool, huh?";
alert(mystr.replace(/<a\b[^>]*>(.*?)<\/a>/i,""));

It's not really foolproof, but maybe it'll do the trick for your purpose...

like image 132
ChristopheD Avatar answered Nov 20 '22 12:11

ChristopheD


Just to clarify, in order to strip link tags and leave everything between them untouched, it is a two step process - remove the opening tag, then remove the closing tag.

txt.replace(/<a\b[^>]*>/i,"").replace(/<\/a>/i, "");

Working sample:

<script>
 function stripLink(txt) {
    return txt.replace(/<a\b[^>]*>/i,"").replace(/<\/a>/i, "");
 }
</script>

<p id="strip">
 <a href="#">
  <em>Here's the text!</em>
 </a>
</p>

<p>
 <input value="Strip" type="button" onclick="alert(stripLink(document.getElementById('strip').innerHTML))">
</p>
like image 14
Paul Worlton Avatar answered Nov 20 '22 10:11

Paul Worlton


Regexes are fundamentally bad at parsing HTML (see Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why). What you need is an HTML parser. See Can you provide an example of parsing HTML with your favorite parser? for examples using a variety of parsers.

like image 3
Chas. Owens Avatar answered Nov 20 '22 11:11

Chas. Owens