7

There's many regex's out there to match a URL. However, I'm trying to match URLs that do not appear anywhere within a <a> hyperlink tag (HREF, inner value, etc.). So NONE of the URLs in these should match:

<a href="http://www.example.com/">something</a>
<a href="http://www.example.com/">http://www.example2.com</a>
<a href="http://www.example.com/"><b>something</b>http://www.example.com/<span>test</span></a>

Any URL outside of <a></a> should be matched.

One approach I tried was to use a negative lookahead to see if the first <a> tag after the URL was an opening <a> or a closing </a>. If it is a closing </a> then the URL must be inside a hyperlink. I think this idea was okay, but the negative lookahead regex didn't work (or more accurately, the regex wasn't written correctly). Any tips are very appreciated.

2

6 Answers 6

5

I was looking for this answer as well and because nothing out there really worked like I wanted it too this is the regex that I created. Obviously since its a regex be aware that this is not a perfect solution.

/(?!<a[^>]*>[^<])(((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?))(?![^<]*<\/a>)/gi

And the whole function to update html is:

function linkifyWithRegex(input) {
  let html = input;
  let regx = /(?!<a[^>]*>[^<])(((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?))(?![^<]*<\/a>)/gi;
  html = html.replace(
    regx,
    function (match) {
      return '<a href="' + match + '">' + match + "</a>";
    }
  );
  return html;
}

2

You can do it in two steps instead of trying to come up with a single regular expression:

  1. Blend out (replace with nothing) the HTML anchor part (the entire anchor tag: opening tag, content and closing tag).

  2. Match the URL

In Perl it could be:

my $curLine = $_; #Do not change $_ if it is needed for something else.
$curLine =~ /<a[^<]+<\/a>//g; #Remove all of HTML anchor tag, "<a", "</a>" and everything in between.
if ( $curLine =~ /http:\/\//)
{
  print "Matched an URL outside a HTML anchor !: $_\n";
}
5
  • If I remove (blend out) the HTML anchors, I won't be able to determine if the URL was originally inside a hyperlink, right? I'm only looking for URLs that are outside hyperlink tags.
    – Ben Amada
    Aug 22, 2009 at 10:09
  • I mean: remove everything from the opening anchor tag till the closing anchor tag. Aug 22, 2009 at 10:13
  • Ah, great solution. I got it working. At first I thought you meant to just remove the beginning and ending tags, but removing the whole tag was the trick. Thank you!!
    – Ben Amada
    Aug 22, 2009 at 10:57
  • -1 You should remove the <a> elements through a proper parser, since HTML is not a regular language.
    – Svante
    Aug 22, 2009 at 11:40
  • 1
    @Svante: I don't think this is fair. Shouldn't it be directed towards the question instead? The question was about matching with regular expressions. Aug 22, 2009 at 17:12
0

You can do that using a single regular expression that matches both anchor tags and hyperlinks:

# Note that this is a dummy, you'll need a more sophisticated URL regex
regex = '(<a[^>]+>)|(http://.*)'

Then loop over the results and only process matches where the second sub-pattern was found.

2
  • This only works for those URLs that are inside the tag, not for those inside an <a> element. Also, it tries to parse a non-regular language with regular expressions.
    – Svante
    Aug 22, 2009 at 11:38
  • @Svante: First, you can easily extend the example to match everything within <a...> and </a>. Then it does the same as the accepted answer, only in a single pass. Second, no, "it" does not try to parse anything but a regular language based on occurrences of HTML-ish strings. There is no need to use a full-featured HTML parser if all you want is find simple pattern in the string. Aug 22, 2009 at 23:09
0

Peter has a great answer: first, remove anchors so that

Some text <a href="http://page.net">TeXt</a> and some more text with link http://a.net

is replaced by

Some text  and some more text with link http://a.net

THEN run a regexp that finds urls:

http://a.net
0

Use the DOM to filter out the anchor elements, then do a simple URL regex on the rest.

0
 ^.*<(a|A){1,1}  ->scan until >a or >A is found
 .*(href|HREF){1,1}\=  -> scan until href= or HREF=
  \x22{1,1}.*\x22  -> accept all characters between two quotes
  > -> look for >
  .+(|){1,1} -> accept description and end anchor tag
  $ -> End of string search


    pattern= "^.*<(a|A){1,1}.*(href|HREF){1,1}.*\=.*\x22{0,1}.*\x22{0,1}.*>.+(|){1,1}$"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.