Adding a Twitter feed via jQuery
Written by Robin Fisher in 89 words
13
Apr
So, as part of my ongoing development of the site, I’ve added a Twitter feed. The instructions here were great for getting a basic feed.
I’ve then expanded on the javascript used to linkify normal links to incorporate links to users and hashtags:
String.prototype.linkify = function() {
var text = this.replace(/([A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/,"<a href='$1'>$1</a>");
var text = text.replace(/(@([\w]+))/g,"<a href='http://twitter.com/$2'>$1</a>");
var text = text.replace(/(#([\w]+))/g,"<a href='http://twitter.com/#search?q=$2'>$1</a>");
return text
};
The first replace deals with normal links. The second deals with usernames by capturing each username and then using backreferences to construct the link. The same method is used for hashtags.