title: Escape regex within string
body: <p>I have a large piece of HTML/CSS/JS which gets served to 3rd party sites via a script tag.  The script tag has an id which I use to match and insert the content at the correct location (no nasty <code>document.write</code>).  However, I've come across a really odd error.  Any line with a regex in it seem to escape itself from the string.  Below is an example of a problem line (one of a few that are causing problems):</p>
  
  <pre><code>+"        // preceding code"
  +"        var remSpc = new RegExp('[\$%,]', 'ig');"
  +"        // following code"
  </code></pre>
  
  <p>The preceding and following lines are correctly escaped, the syntax for the middle line looks correct to me, but it's throwing an "unexpected end of input" error and WebKit Inspector is showing it as being unescaped.  Screen below to show what I mean by unescaped:</p>
  
  <p><img src="https://i.stack.imgur.com/Uk1B6.png" alt="enter image description here"></p>
  
  <p>The purpose of the regex is to remove special characters from $ amounts and percentages (example values might be "$1,000" or "5.5%".  I've tried not using a RegExp object and instead just doing `/[\$%,]/ig and I get exactly the same problem.  </p>
  
  <p><strong>N.B the issue is not with this regex in particular, I have a few regexs and every line containing one is behaving the same way.</strong></p>