Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Last active August 29, 2015 14:07
Show Gist options
  • Save monkeymonk/9568b8cd711f3ab3b00d to your computer and use it in GitHub Desktop.
Save monkeymonk/9568b8cd711f3ab3b00d to your computer and use it in GitHub Desktop.
AngularJS `wrapText` filter
angular.module('utils.filters', [])
.filter('wrapText', wrapText);
function wrapText($sce) {
return function (source, needle, wrap, strict) {
var regex;
if (typeof needle === 'string') {
regex = new RegExp(needle, "gi");
} else {
regex = needle;
}
if (source.match(regex)) {
source = source.replace(regex, function (match) {
return $('<i></i>').append($(wrap).text(match)).html();
});
}
if (!strict) {
source = $sce.trustAsHtml(source);
}
return source;
};
} // wrapText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment