Regex for replacing URL with anchor tag

Despite there being 1001 regexes on the web for matching a URL in a string and then replacing it with an anchor tag (commonly used in chat applications etc.), I couldn’t find one which did exactly what I wanted, so I thought I’d share my regex for replacing URLs with anchor tags.

Firstly, the problem with all the other regexs I found. Take for example the following text block:

you should have a look at <a href='http://example1.com'>http://example1.com</a>, and http://example2.com

All of the regexes I found for matching and replacing URLs with anchor tags would have transformed my text block into:

you should have a look at <a href='<a href='http://example1.com'>http://example1.com</a>'>http://example1.com</a>, and <a href='http://example2.com'>http://example2.com</a>

None of the example regexes I found coped with the possibility that some URLs may already be contained in an anchor tag, as in the example above, or in other instances where you don’t want to insert an anchor tag, such as: <img src='http://example.com/logo.png'/>

The regex and code I am now using to get around this problem is (in Javascript):

var mystring = "text with some URLs in";
var myregex = /(^|[^'">])((ftp|http|https):\/\/(\S+))(\b|$)/gi;
var newstring = mystring.replace(myregex ,"$1<a href='$2'>$2</a>");

All appears to be working well so far, but I’m sure my regex is far from perfect. This regex doesn’t check that the URL is really well formed, for example valid top level domain etc.

So please post any possible improvements below, in the meantime I hope it’s useful :-)

Leave a Reply