title: Create RegExps on the fly using string variables
body: <p>Say I wanted to make the following re-usable:</p>
  
  <pre><code>function replace_foo(target, replacement) {
     return target.replace("string_to_replace",replacement);
  }
  </code></pre>
  
  <p>I might do something like this:</p>
  
  <pre><code>function replace_foo(target, string_to_replace, replacement) {
     return target.replace(string_to_replace,replacement);
  }
  </code></pre>
  
  <p>With string literals this is easy enough. But what if I want to get a little more tricky with the regex? For example, say I want to replace everything <em>but</em> <code>string_to_replace</code>. Instinctually I would try to extend the above by doing something like:</p>
  
  <pre><code>function replace_foo(target, string_to_replace, replacement) {
     return target.replace(/^string_to_replace/,replacement);
  }
  </code></pre>
  
  <p>This doesn't seem to work. My guess is that it thinks <code>string_to_replace</code> is a string literal, rather than a variable representing a string. Is it possible to create JavaScript regexes on the fly using string variables? Something like this would be great if at all possible:</p>
  
  <pre><code>function replace_foo(target, string_to_replace, replacement) {
     var regex = "/^" + string_to_replace + "/";
     return target.replace(regex,replacement);
  }
  </code></pre>