Skip to content

Instantly share code, notes, and snippets.

@adammagana
Created March 19, 2012 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammagana/2121608 to your computer and use it in GitHub Desktop.
Save adammagana/2121608 to your computer and use it in GitHub Desktop.
Replaces Twitter username, hashtag, and link text with links.
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