Ever had to display user submitted content on a web site you’ve created? Then you’ve probably had a requirement to display urls in submitted text as actual live links.

Despite the fact that this must have been done a gazillion times before, I couldn’t find any site that would give me a ready to use regular expression to turn text bits that look like URLs into actual hyperlinks. But some Googling, a couple of hours and regex-refresher later I had something that appears to work quite well:

in = "go check out www.sfpeter.com or http://sfpeter.com"
in.gsub!(/([^/])(www.([w/_.~])*)/, '\\1<a href="http://\\2" target="_blank">\\2</a>')
in.gsub!(/([^"])(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)/,
    '\\1<a href="\\2" target="_blank">\\2</a>')

this will turn both links in the input string into active links (that will open in a new browser window). It will consider strings that either start with www.<something> or http(s)://<something> to be a web URL. Users often don’t add ‘http://’ to links they submit, so this will still work as long as their links start with ‘www.’ .

One of the tricky issues was making sure that after replacing www.sfpeter.com with an active hyperlink, the second regex wouldn’t come by and substitute the link again, so you end up with something like this

<a href="<a href="http://www.sfpeter.com">www.sfpeter.com</a>">www.sfpeter.com</a>

To prevent this, the second regex ignores any urls that are preceded by a double quote.

This is Ruby code of course, but the patterns should work fine in any language. I could take it one step further and hyperlink anything that ends in .com, .net or other well known top level domains, so just sfpeter.com would be a match too, but I’ll leave that for later. I’m open to suggestions for improvement!