title: AngularJS: Filter by regex
body: <p>I want to filter a list of elements by regex (user will type a regex in the input search field) using AngularJS filter.</p>
  
  <p>I wrote everything that seemed to be necessary for me, but I can't manage to make the regex work correctly.</p>
  
  <p>Here is what I've done so far: </p>
  
  <p>View.html</p>
  
  <pre><code>&lt;div ng-controller="listCtrl"&gt;
  &lt;input type="text" placeholder="search by any" ng-model="search.$"&gt;
  &lt;input type="text" placeholder="search by fist name" ng-model="search.fname"&gt;
  &lt;input type="text" placeholder="search by last name" ng-model="search.lname" &gt;
  &lt;input type="text" placeholder="search by tel" ng-model="search.tel" &gt;
  &lt;input type="text" placeholder="search by date" ng-model="search.date"&gt;
  &lt;table&gt;
          &lt;tr ng-repeat="user in users|regex : search as res"&gt;
             &lt;td&gt;{{user.fname}}&lt;/td&gt;
              &lt;td&gt;{{user.lname.toUpperCase()}}&lt;/td&gt;
              &lt;td&gt;{{user.tel}}&lt;/td&gt;
              &lt;td&gt;{{user.date | date: 'EEE'}}&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr ng-if="res.length &lt; 1"&gt;
              &lt;td&gt;No elements found...&lt;/td&gt;
          &lt;/t&gt;
          &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>app.js</p>
  
  <pre><code>...
  app.filter('regex', function() {
  
    return function(input, property) {
     var patt;
     var field;
     var out = [];
     if(input === undefined || property === undefined) {
      return out;
    }
  
    angular.forEach(property, function(key, value) {
     patt = new RegExp(key);   
     field = value;
   });
  
    for (var i = 0; i &lt; input.length; i++){
      if(patt.test(input[i][eval(field)]))
        out.push(input[i]);
    }      
    return out;
  };
  });
  ...
  </code></pre>
  
  <p>The <code>listCtrl</code> will just add some elements to <code>$scope</code>. </p>
  
  <p>What is wrong here?</p>