Skip to content

Instantly share code, notes, and snippets.

@Radagaisus
Created September 1, 2011 02:20
Show Gist options
  • Save Radagaisus/1185289 to your computer and use it in GitHub Desktop.
Save Radagaisus/1185289 to your computer and use it in GitHub Desktop.
add links to urls, users and hashtags for text from Twitter
function twitterify(text) {
return text
// URLS
.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
"<a href='$1'>$1</a>")
// Hashtags
.replace(/\B#([^ ]+)\b/ig,
"<a href='http://twitter.com/#!/search?q=%23$1'>#$1</a>")
// Users
.replace(/\B@([^ ]+)\b/ig,"<a href='http://twitter.com/#!/$1'>@$1</a>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment