title: In Javascript, how can I perform a global replace on string with a variable inside '/' and '/g'?
body: <p>I want to perform a global replace of string using String.replace in Javascript.</p>
  <p>In the documentation I read that I can do this with /g, i.e. for example;</p>
  <pre><code>var mystring = mystring.replace(/test/g, mystring);
  </code></pre>
  <p>and this will replace all occurrences inside mystring. No quotes for the expression.</p>
  <p>But if I have a variable to find, how can I do this without quotes?</p>
  <p>I've tried something like this:</p>
  <pre><code>var stringToFind = &quot;test&quot;;
  </code></pre>
  <p>//first try</p>
  <pre><code>mystring = mystring.replace('/' + stringToFind + '/g', mystring);
  </code></pre>
  <p>//second try, not much sense at all</p>
  <pre><code>mystring = mystring.replace(/stringToFind/g, mystring);
  </code></pre>
  <p>but they don't work. Any ideas?</p>