title: How do you access the matched groups in a JavaScript regular expression?
body: <p>I want to match a portion of a string using a <a href="https://en.wikipedia.org/wiki/Regular_expression" rel="noreferrer">regular expression</a> and then access that parenthesized substring:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    var myString = "something format_abc"; // I want "abc"
  
      var arr = /(?:^|\s)format_(.*?)(?:\s|$)/.exec(myString);
  
      console.log(arr);     // Prints: [" format_abc", "abc"] .. so far so good.
      console.log(arr[1]);  // Prints: undefined  (???)
      console.log(arr[0]);  // Prints: format_undefined (!!!)</code></pre>
  </div>
  </div>
  </p>
  <p>What am I doing wrong?</p>
  <hr />
  <p>I've discovered that there was nothing wrong with the regular expression code above: the actual string which I was testing against was this:</p>
  <pre><code>&quot;date format_%A&quot;
  </code></pre>
  <p>Reporting that &quot;%A&quot; is undefined seems a very strange behaviour, but it is not directly related to this question, so I've opened a new one, <em><a href="https://stackoverflow.com/questions/432826/why-is-a-matched-substring-returning-undefined-in-javascript">Why is a matched substring returning &quot;undefined&quot; in JavaScript?</a></em>.</p>
  <hr />
  <p>The issue was that <code>console.log</code> takes its parameters like a <code>printf</code> statement, and since the string I was logging (<code>&quot;%A&quot;</code>) had a special value, it was trying to find the value of the next parameter.</p>