Created
July 18, 2013 03:53
-
-
Save tylerpearson/6026612 to your computer and use it in GitHub Desktop.
a simple jQuery plugin to add links to the text of tweets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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