Tweetify

This is David Walsh’s String.Tweetify in jQuery.

$.fn.tweetify = function() {
	this.each(function() {
		$(this).html(
			$(this).html()
				.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'$1')
				.replace(/(^|\s)#(\w+)/g,'$1#$2')
				.replace(/(^|\s)@(\w+)/g,'$1@$2')
		);
	});
	return $(this);
}

$("p").tweetify();

Before:
<p>@seanhood have you seen this http://icanhascheezburger.com/ #lol</p>

After:
<p><a href="http://twitter.com/seanhood">@seanhood</a> have you seen this
<a href="http://icanhascheezburger.com/">http://icanhascheezburger.com/</a>
<a href="http://search.twitter.com/search?q=%23lol">#lol</a></p>

Demo

Tags:

Comments

Ahh, you went with HTML. My thinking was “What do I use? HTML? Text? What if it’s a form? Value?” That was my frustration. Nice work!

Drop A Comment