public
Last active — forked from 140bytes/LICENSE.txt

linkify @mentions and #hashtags in a tweet

  • Download Gist

This should be a byte shorter:

function(a){return a.replace(/[@#]\w+/g,function(b,c){c="twitter.com/";return b.link("//"+(b[a="search"]("#")?c:a+"."+c+a+"?q=")+b)})}

Good catch! Fixed. That byte may be what we need to fix @mention links from including the @.

Awesome work Jed, although I have some changes:
1) It matches email address too, so need \B to indicate a word boundary
2) It contains the @ in the mention URL (which works, but it isn't perfect)

I've made some changes to fix these issues and it comes in at 138 bytes:

function(a){return a.replace(/\B[@#]\w+/g,function(b){return b.link('//twitter.com/'+(b[a='search']('#')?'':a+'?q=%23')+b.substring(1))})}

ha, who tweets email addresses?

(but seriously, thanks! fixed.)

You need to use \B instead of \b. \b matches boundaries where a word and a non-word character met (or vice versa). For example, 'example@example.com'.replace(/\b/g,'|') returns |example|@|example|.|com|, making all boundaries visible. In your case, you need it the other way around. There should not be a boundary in front of the @. Hope that helps.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.