I want to use regex in ng-repeat. I have tried the following code but its not working.
<
Gloopy's answer is spot on. I implemented it in my site but I was getting an error,
'input' is undefined
This occurred because I was sometimes looping over nothing. I fixed this by adding a conditional to return the empty out array if input was undefined.
var myApp = angular.module('myApp', []);
myApp.filter('regex', function() {
return function(input, field, regex) {
var patt = new RegExp(regex);
var out = [];
if(input === undefined) {
return out;
}
for (var i = 0; i < input.length; i++){
if(patt.test(input[i][field]))
out.push(input[i]);
}
return out;
};
});
Hopefully this will help someone who might be having the same problem