Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@jhgaylor jhgaylor commented Nov 13, 2013

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
You can’t perform that action at this time.