Tweetify Plain Text


/ Published in: JavaScript
Save to your folder(s)

This is 3 lines of javascript code to tweetify plain text returned by Twitter API

Input : RT @TTintheCloud: How much are free #cloudcomputing trial offers from Google, Amazon and Microsoft actually WORTH? http://bit.ly/i4w4qa

Output : RT <a href="http://twitter.com/TTintheCloud">@TTintheCloud</a>: How much are free <a href="http://search.twitter.com/search?q=&amp;tag=cloudcomputing&amp;lang=all">#cloudcomputing</a> trial offers from Google, Amazon and Microsoft actually WORTH? <a href="http://bit.ly/i4w4qa">http://bit.ly/i4w4qa</a>


Copy this code and paste it in your HTML
  1. function tweetify(text) {
  2. text = text.replace(/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:(?:[^\s()<>.]+[.]?)+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?���«���»�¢ï¿½ï¿½�¢ï¿½ï¿½�¢ï¿½ï¿½�¢ï¿½ï¿½]))/gi,
  3. "<a target=_blank href=$1>$1</a>");
  4. text = text.replace(/[\@]+([A-Za-z0-9-_]+)/gi, "<a target=_blank href=http://twitter.com/$1>@$1</a>");
  5. return text.replace(/(?:^| )[\#]+([A-Za-z0-9-_]+)/gi, " <a target=_blank href=http://search.twitter.com/search?q=&tag=$1&lang=all>#$1</a>");
  6. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.