Created
June 6, 2013 06:55
-
-
Save ZaneA/5719772 to your computer and use it in GitHub Desktop.
A couple of filters for AngularJS.
This file contains hidden or 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
app.filter('escape', function () { | |
return function (text) { | |
if (!text) return ''; | |
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | |
}; | |
}); | |
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