title: Wrap a regular expression so that it only matches the whole string
body: <p>Related: <a href="https://stackoverflow.com/questions/6298566/regex-match-whole-string">Regex - Match whole string</a></p>
  
  <p>I want a given regexp to match the whole string. For example, when given a regexp <code>/abc/</code>, it should only match string <code>"abc"</code> but not <code>"abcd"</code>. I have searched the above question, which has somehow a similar situation than mine. The difference here is that the regexp is not directly written in my code. So I can't change <code>/abc/</code> to <code>/^abc$/</code> directly in the source code.</p>
  
  <p>Let's say, I want a function which takes two arguments: a 'regexp' (e.g. <code>/abc/</code>) and a <code>string</code> (e.g. <code>'abc'</code>). The function returns matched result if and only if the given regexp matches the <em>whole</em> string, but returns <code>null</code> otherwise.</p>
  
  <p>What I'm trying:</p>
  
  <pre><code>function match(regexp, string) {
    var parse = /\/(.*)\/(.*)/.exec(regexp);
    var reg = new RegExp('^' + parse[1] + '$', parse[2]);
    return string.match(reg);
  }
  </code></pre>
  
  <p>Is this code correct? Any better way to do so?</p>