Skip to content

Instantly share code, notes, and snippets.

@ahmednuaman
Created December 4, 2012 19:39
Show Gist options
  • Save ahmednuaman/4207874 to your computer and use it in GitHub Desktop.
Save ahmednuaman/4207874 to your computer and use it in GitHub Desktop.
Linkify a tweet
String.prototype.linkify = function()
{
var str = this.toString();
str = str.replace(/(https?:\/\/[^\s]+)/gim, '<a href="$1">$1</a>', str);
str = str.replace(/([#|@][^\s]+)/gim, '<a href="http://twitter.com/$1">$1</a>', str);
return str;
}
'Here\'s an @test and an #test'.linkify() // -> Here's an <a href="http://twitter.com/@test">@test</a> and an <a href="http://twitter.com/#test">#test</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment