Skip to content

Instantly share code, notes, and snippets.

@ZaneA
Created June 6, 2013 06:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZaneA/5719772 to your computer and use it in GitHub Desktop.
Save ZaneA/5719772 to your computer and use it in GitHub Desktop.
A couple of filters for AngularJS.
app.filter('escape', function () {
return function (text) {
if (!text) return '';
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
};
});
app.filter('linkify', function () {
var domainRegexp = /(https?|ftp):\/\/(.*)/;
var urlPatternReplacer = function (match, contents, offset, s) {
return '<a href="' + match + '">' + match.match(domainRegexp)[2] + '</a>';
};
return function (text) {
if (!text) return '';
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
return text.replace(urlPattern, urlPatternReplacer);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment