-
-
Save jed/962823 to your computer and use it in GitHub Desktop.
linkify @mentions and #hashtags in a tweet
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
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)})} |
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:
- It matches email address too, so need \B to indicate a word boundary
- 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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should be a byte shorter: