Mike-Ward.Net

Converting URLs to Hyperlinks in JavaScript

I hunted around for a JavaScript method to convert URLs in a text stream to hyper-links and came up short. I wrote this quickie method that works for the limited data I’ve thrown at it. There are likely more robust methods but darn if I could find one.

function convertUrlsToLinks(text)
{
    var matchUrl = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi
    return text.replace(matchUrl, '[<a href="$&">$&</a>]($&)');
}

OK, JavaScript guru’s. Is there a better way?

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Mithril Rising
Tweetz Release 1.2.4 - Screen Off Detection
Tweetz Desktop Support for 280 Characters
Quick Tip - Clear ReSharper's Cache to Fix False Errors
Quick Tip - Refined GitHub
Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
More... (1080)

Donate