Skip to content

Instantly share code, notes, and snippets.

@tylerpearson
Created July 18, 2013 03:53
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 tylerpearson/6026612 to your computer and use it in GitHub Desktop.
Save tylerpearson/6026612 to your computer and use it in GitHub Desktop.
a simple jQuery plugin to add links to the text of tweets
// Twlinkify
// an absurdly simple jQuery plugin to linkify the text of tweets
;(function($) {
// from http://stackoverflow.com/questions/8020739/regex-how-to-replace-twitter-links
function addTwitterLinks(text) {
var e = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
text = text.replace(e, "<a href='$1' target='_blank'>$1</a>");
e = /(^|\s)#(\w+)/g;
text = text.replace(e, "$1<a href='http://search.twitter.com/search?q=%23$2' target='_blank'>#$2</a>");
e = /(^|\s)@(\w+)/g;
text = text.replace(e, "$1<a href='http://www.twitter.com/$2' target='_blank'>@$2</a>");
return text;
}
$.fn.twlinkify = function() {
return this.each(function() {
var $el = $(this);
$el.html(addTwitterLinks($el.text()));
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment