title: How do I escape content that might include a quotation mark?
body: <p>I'm wanting to capture my search terms and pass them to a JavaScript variable, but I don't know how to handle quotes that might come through.</p>
  
  <p>Here's what I have currently:</p>
  
  <pre><code>var searchTerms = "&lt;!--#echo var="terms"--&gt;";
  var pattern = / /g;
  newSearchTerms = searchTerms.replace(/[^a-zA-Z 0-9]+/g,'');
  var searchStr=newSearchTerms.replace(pattern,"_");
  </code></pre>
  
  <p>I'm concerned that should "terms" contain double quotes (or an apostrophy if I use single quotes in the JS) then my function will fail.</p>
  
  <p>How do I escape the string before it gets into script?</p>
  
  <p>Thanks,<br />
  Steve</p>
  
  <hr>
  
  <p>Edit/answer: I ended up doing this by moving this to an external script that captured and parsed the querystring rather than echoing it in the HTML.</p>