Skip to content

Instantly share code, notes, and snippets.

@ryndel
Created October 14, 2012 01:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryndel/3886867 to your computer and use it in GitHub Desktop.
Save ryndel/3886867 to your computer and use it in GitHub Desktop.
Handlebars.js: linkify helper #cc #handlebars
Handlebars.registerHelper('linkify', function (text) {
text = text.replace(/(https?:\/\/\S+)/gi, function (s) {
return '<a href="' + s + '">' + s + '</a>';
});
text = text.replace(/(^|)@(\w+)/gi, function (s) {
return '<a href="http://twitter.com/' + s + '">' + s + '</a>';
});
text = text.replace(/(^|)#(\w+)/gi, function (s) {
return '<a href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>';
});
return new Handlebars.SafeString(text);
});
@jhgaylor
Copy link

I was having trouble with trailing punctuation (http://google.com,) so I changed to this regex

/(http|ftp|https)://([\w-]+(.[\w-]+)+)([\w.,@?^=%&:/+#-]*[\w@?^=%&/+#-])?/gi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment