Hyperlinks for your twitter posts

Here’s a quick Javascript to take a string and automatically add hyperlinks to any twitter tags it sees. This could be useful if you were writing a web page for users to enter tweets. It will take a string such as “I gave @siliconsparrow a hug”, detect the at-tag and wrap it in the appropriate hyperlink.

Here’s a demo page where you can see it in action.

function addTwitterLinks(text) {
 return text.replace(/[\@\#]([a-zA-z0-9_]*)/g,
   function(m,m1) {
     var t = '<a href="http://twitter.com/';
     if(m.charAt(0) == '#')
       t += 'hashtag/';
     return t + encodeURI(m1) + '" target="_blank">' + m + '</a>';
   });
}