Skip to content

Instantly share code, notes, and snippets.

@elijahr
Created June 28, 2011 18:08
Show Gist options
  • Save elijahr/1051754 to your computer and use it in GitHub Desktop.
Save elijahr/1051754 to your computer and use it in GitHub Desktop.
parse tweets
// based on http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript
String.prototype.parseTweet = function(){
return this
.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
return url.link(url);
})
.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
var username = u.replace("@","")
return u.link("http://twitter.com/"+username);
})
.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
var tag = t.replace("#","%23")
return t.link("http://search.twitter.com/search?q="+tag);
});
}
// just give your bottom-most divs (those with only text, no child elements)
// a class of 'tweet' and you'll be all set
window.onload = function(){
var i, tweet_divs, div;
tweet_divs = document.getElementsByClassName('tweet');
for (i=0; i<tweet_divs.length; i++) {
div = tweet_divs[i];
div.innerHTML = div.innerHTML.toString().parseTweet();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment