Created
March 19, 2012 17:57
-
-
Save adammagana/2121608 to your computer and use it in GitHub Desktop.
Replaces Twitter username, hashtag, and link text with links.
This file contains hidden or 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
function parseTweet(tweet) { | |
var urlRegex = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, | |
userRegex = /[@]+[A-Za-z0-9-_]+/g, | |
hashRegex = /[#]+[A-Za-z0-9-_]+/g; | |
tweet = tweet.replace(urlRegex, function(url) { | |
return url.link(url); | |
}); | |
tweet = tweet.replace(userRegex, function(username) { | |
var rawUsername = username.replace("@", ""); | |
return username.link("http://twitter.com/#!/"+rawUsername); | |
}); | |
tweet = tweet.replace(hashRegex, function(hash) { | |
var encodedHash = hash.replace("#", "%23"); | |
return hash.link("http://search.twitter.com/search?q="+encodedHash); | |
}); | |
return tweet; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment