title: Get all substrings inside a string that match a regular expression
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/520611/how-can-i-match-multiple-occurrences-with-a-regex-in-javascript-similar-to-phps">How can I match multiple occurrences with a regex in JavaScript similar to PHP&#39;s preg_match_all()?</a>  </p>
  </blockquote>
  
  
  
  <p>In Javascript, is it possible to find the starting and ending indices of all substrings within a string that match a regular expression?</p>
  
  <p>Function signature:</p>
  
  <pre><code>function getMatches(theString, theRegex){
      //return the starting and ending indices of match of theRegex inside theString
      //a 2D array should be returned
  }
  </code></pre>
  
  <p>For example:</p>
  
  <pre><code>getMatches("cats and rats", /(c|r)ats/);
  </code></pre>
  
  <p>should return the array <code>[[0, 3], [9, 12]]</code>, which represents the starting and ending indices of "cats" and "rats" within the string.</p>