-
-
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 // the text of the tweet to be enlinked | |
){ | |
return a.replace( // replace | |
/\b[@#]\w+/g, // any mention/hashtag | |
function( | |
b, // the matched mention/hashtag | |
c // placeholder | |
){ | |
c = "twitter.com/"; // the core twitter url | |
return b.link( // put the match in a link to | |
"//" + ( // the "//" protocol, + | |
b[a = "search"] // match the url, reusing for "search" | |
("#") // if the match starts with "#" | |
? c // twitter.com/, or otherwise | |
: a + "." + c + // search.twitter.com/ + | |
a + "?q=" // search?q= | |
) + // and then finally, + | |
b // the mention/hashtag. | |
) | |
} | |
) | |
} |
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(/\b[@#]\w+/g,function(b,c){c="twitter.com/";return b.link("//"+(b[a="search"]("#")?c:a+"."+c+a+"?q=")+b)})} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "enlink", | |
"keywords": ["twitter", "tweet", "link", "mention", "hashtag"] | |
} |
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, '[email protected]'.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
ha, who tweets email addresses?
(but seriously, thanks! fixed.)