title: How can I write an angularjs filter to search for string fragments/sequences of strings
body: <p>I'm trying to write an angularjs custom filter that checks whether an array of countries contains a search string entered by the user. </p>
  
  <p>The string can consist of one letter (e.g. 'E'), or a fragment of n-letters (e.g. 'lan') or an entire word (e.g. 'England'). </p>
  
  <p>In every case, all countries containing that one letter or that fragment should be returned, so 'E' would return 'England', 'Estonia' etc. while 'lan' would return 'England', 'Ireland', etc.</p>
  
  <p>So far my filter returns the entire country or single letters but I'm having difficulty with string fragments:</p>
  
  <ol>
  <li><p>HTML Template:</p>
  
  <pre><code>&lt;input ng-model="filter.text" type="search" placeholder="Filter..."/&gt;
  
  &lt;ul&gt;
     &lt;li ng-repeat="item in data | listfilter:filter.text"&gt;
  &lt;/ul&gt;
  </code></pre></li>
  <li><p>angularJS</p>
  
  <pre><code>angular.module('sgComponents').filter('listfilter',[ function () {
  return function(items, searchText) {
      var filtered = [];            
  
      angular.forEach(items, function(item) {
          if(item.label === searchText) { // matches whole word, e.g. 'England'
              filtered.push(item);
          }
          var letters = item.label.split('');
          _.each(letters, function(letter) {
              if (letter === searchText) { // matches single letter, e.g. 'E'
                  console.log('pushing');
                  filtered.push(item);
              }
          });
          // code to match letter fragments, e.g. 'lan'
      });
      return filtered;
  };
  }]);
  </code></pre></li>
  </ol>