title: Why javascript string match includes undefined
body: <p>I have a regex that is more or less used like this:</p>
  
  <pre><code>'(801) 555-1234'.match(/^(1[-. ]?)?\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/)
  </code></pre>
  
  <p>For some reason this returns</p>
  
  <pre><code>["(801) 555-1234", undefined]
  </code></pre>
  
  <p>If I add the global flag to the regex (e.g. <code>...{4}$/g</code>), the undefined value drops out and I get</p>
  
  <pre><code>["(801) 555-1234"]
  </code></pre>
  
  <p>I'd prefer not to use the g flag if it's not necessary (which it would seem to me it's not, since the regex begins with ^ and ends with $).</p>
  
  <p>P.S. ignore the quality of the regex for it's purpose of matching phone numbers.  It may not be ideal, but is from code I'm maintaining.  Mostly I'm interested in the ^...$ and the presence/absence of the flag and the undefined value.</p>
  
  <p>Why is <code>undefined</code> showing up, and why does the flag make the difference?</p>