title: How to write a Jasmine custom matcher
body: <p>I am trying to write a Custom Matcher that tests if a string contains a special number of occurrences of a different string. This is what I did.</p>
  
  <pre><code>var matcher = {
      toContainTimes: function (expected, num) {
          return {
              compare: function(actual){
                  actual.match(new RegExp(expected, "g") || []).length == num;
              }
          }
      }
  }
  </code></pre>
  
  <p>But I get an error when executing this:</p>
  
  <pre><code>TypeError: Cannot read property 'pass' of undefined
  </code></pre>
  
  <p>My test looks like this:</p>
  
  <pre><code>expect(queryString).toContainTimes("&amp;", 2);
  </code></pre>
  
  <p>It should return true if the string "&amp;" occurs exactly twice in queryString.
  What did I do wrong?</p>