Skip to content

@jed /LICENSE.txt forked from 140bytes/LICENSE.txt
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
linkify @mentions and #hashtags in a tweet
function(a){return a.replace(/[@#]\w+/g,function(b,c){a="search";c="twitter.com/";return b.link("//"+(b[a]("#")?c:a+"."+c+a+"?q=")+b)})}
@Pet3ris

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)})}
@jed
Owner

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

@jamiebicknell

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))})}
@jed
Owner
jed commented

ha, who tweets email addresses?

(but seriously, thanks! fixed.)

@maettig

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.