Alex Rivera | Logout

How to use Regex with ng-repeat in AngularJs?

Asked 2012-08-21T00:18:00.340
18

I want to use regex in ng-repeat. I have tried the following code but its not working.

<div ng-repeat="user in users | filter:{'type':/^c5$/}"></div>

I have users array and I only want to display the users with type c5.

If I use

filter:{'type':'c5'} 

then its displaying the users with type "ac5x" too, because its contains c5.

How can I solve this problem? Maybe there is another solution.

Thank You!

Edit
Report

1 Answer

16

You can use function in filter expression. So basically, you can do any filtering possible with javascript.

<li ng-repeat="name in names | filter:myFilter"> {{ name }}

In controller:

$scope.myFilter = function(user) {
   return /^c5$/.test(user.type);
};
answered 2012-08-21T00:43:34.217

Your Answer