Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created October 20, 2011 23:17
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 fideloper/1302691 to your computer and use it in GitHub Desktop.
Save fideloper/1302691 to your computer and use it in GitHub Desktop.
Linkify Twitter feed (@usernames, #hashtags and links)
String.prototype.linkify_tweet = function() {
var tweet = this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(url) {
var wrap = document.createElement('div');
var anch = document.createElement('a');
anch.href = url;
anch.target = "_blank";
anch.innerHTML = url;
wrap.appendChild(anch);
return wrap.innerHTML;
});
tweet = tweet.replace(/(^|\s)@(\w+)/g, '$1@<a href="http://www.twitter.com/$2" target="_blank">$2</a>');
return tweet.replace(/(^|\s)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment