RegEx to convert urls in text to links (JavaScript)

Here’s a quick tip.  If you have text that contains urls (i.e. tweets) and want the urls in the text to appear as actual links via HTML, use the following RegEx:

var message = "Here is some tweet text.  Check out: http://www.google.com";

var exp = /(b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/ig;
message = message.replace(exp, "<a href='$1' target='_blank'>$1</a>");

Source: http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links

One thought on “RegEx to convert urls in text to links (JavaScript)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s