I have a field in my AngularJS scope called rid
This is always a 4 letter code such as 1A22 or 4D23 (number, letter, number, number)
I want to filter out (i.e not show) any that meet certain conditions
Now I know I can build individual filters onto my ng-repeat (service in services), but i think the way forward is a nice custom filter where I can feed in an array of conditions that i want to filter out. I suspect a string of regex conditions is the answer
/^2k|5|1C*/
Can anyone advise the best way to approach this custom filter? Thanks
EDIT : This was my solution, regex from the accepted answer and the following filter
myApp.filter('regex', function() {
return function(input, field, regex) {
var patt = new RegExp(regex);
var out = [];
for (var i = 0; i < input.length; i++){
if(!patt.test(input[i][field]))
out.push(input[i]);
}
return out;
};
});
then I just use this in my ng-repeat
regex:'rid':'^(?:2K|5|1C)[A-Z0-9]+$'"
You were not so far. This regex will match any text starting with 2K
, 5
or 1C
:
^(?:2K|5|1C)[A-Z0-9]+$
If you want to make sure the length is 4, you can use this one:
^(?=[A-Z0-9]{4}$)(?:2K|5|1C)[A-Z0-9]+$