Skip to content

Instantly share code, notes, and snippets.

@danro
Created October 14, 2011 17:35
Show Gist options
  • Save danro/1287763 to your computer and use it in GitHub Desktop.
Save danro/1287763 to your computer and use it in GitHub Desktop.
Parse links & usernames in twitter text.
// parses the text in a tweet and adds links to users, hash tags and urls
parseTweet: function (text) {
// parse urls
text = text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9_-]+\.[A-Za-z0-9_:%&~\?\/.=-]+/g, function(url) {
return url.link(url)
})
// parse usernames
text = text.replace(/[@]+[A-Za-z0-9_-]+/g, function(u) {
var username = u.replace("@","")
return u.link("http://twitter.com/"+username)
})
// parse hashtags
text = text.replace(/[#]+[A-Za-z0-9_-]+/g, function(t) {
var tag = t.replace("#","%23")
return t.link("http://search.twitter.com/search?q="+tag)
})
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment