title: javascript - use the filter and match methods on an array to find exact matches
body: <p>I'm using a filter function to find JSON objects in an array called <code>arrayList</code>, which contains for example:</p>
  
  <pre><code>[
   {
     "name": "Markdown",
     "extension": "(.md)"
   },
   {
     "name": "MultiMarkdown",
     "extension": "(.mmd, .txt)"
   }
  ]
  </code></pre>
  
  <p>I'm using the following code to search on the <code>name</code> field and find a match for an object with a certain name:</p>
  
  <pre><code>findFormat = (name) =&gt; {
    let format = arrayList.filter(function (el) {
      let pattern = new RegExp(name);
      return el.name.match(pattern);
    });
    return format.name
  }
  </code></pre>
  
  <p>So if I'm searching for an object that has the name "Markdown" then this string gets passed via the <code>name</code> parameter in the function above, and the <code>RegExp</code> expression resolves to <code>/Markdown/</code>.</p>
  
  <p>For the filter function itself, I've implemented the polyfill described at the bottom of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="nofollow noreferrer">MDN documentation</a>:</p>
  
  <pre><code>if (!Array.prototype.filter)
          Array.prototype.filter = function(func, thisArg) {
              'use strict';
              if ( ! ((typeof func === 'Function') &amp;&amp; this) )
                  throw new TypeError();
  
              var len = this.length &gt;&gt;&gt; 0,
                  res = new Array(len), // preallocate array
                  c = 0, i = -1;
              if (thisArg === undefined)
                  while (++i !== len)
                      // checks to see if the key was set
                      if (i in this)
                          if (func(t[i], i, t))
                              res[c++] = t[i];
                          else
                              while (++i !== len)
                                  // checks to see if the key was set
                                  if (i in this)
                                      if (func.call(thisArg, t[i], i, t))
                                          res[c++] = t[i];
  
              res.length = c; // shrink down array to proper size
              return res;
          };
  </code></pre>
  
  <p>For the regular expression I'm not using any special flags like <code>i</code> because I want to find an exact match for "Markdown". However it keeps returning instead <code>MultiMarkdown</code>. I had thought that the filter function above found only exact matches, and not substrings.</p>
  
  <p><strong>Question</strong>: is there a way I can filter for only exact matches, using the <code>filter</code> and <code>match</code> methods? Wouldn't the polyfill above return an exact match?</p>