Created
October 14, 2011 17:35
-
-
Save danro/1287763 to your computer and use it in GitHub Desktop.
Parse links & usernames in twitter text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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