Last active
August 29, 2015 14:07
-
-
Save monkeymonk/9568b8cd711f3ab3b00d to your computer and use it in GitHub Desktop.
AngularJS `wrapText` filter
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
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