Skip to content

Instantly share code, notes, and snippets.

@dmadisetti
Forked from jakemmarsh/filters.js
Last active August 29, 2015 14:11
Show Gist options
  • Save dmadisetti/20c17e0ec8d1e834010f to your computer and use it in GitHub Desktop.
Save dmadisetti/20c17e0ec8d1e834010f to your computer and use it in GitHub Desktop.
app.filter('parseUrl', function() {
var //URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
//Change email addresses to mailto:: links.
replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
return function(text, target, otherProp) {
if(!text) return;
text = text.replace(replacePattern1, "<a href=\"$1\" target=\"_blank\">$1</a>");
text = text.replace(replacePattern2, "$1<a href=\"http://$2\" target=\"_blank\">$2</a>");
text = text.replace(replacePattern3, "<a href=\"mailto:$1\">$1</a>");
return text;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment