title: Javascript string replace with regex to strip off illegal characters
body: <p>Need a function to strip off a set of illegal character in javascript: <code>|&amp;;$%@"&lt;&gt;()+,</code></p>
  
  <p>This is a classic problem to be solved with regexes, which means now <a href="http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html" rel="noreferrer">I have 2 problems</a>.</p>
  
  <p>This is what I've got so far:</p>
  
  <pre><code>var cleanString = dirtyString.replace(/\|&amp;;\$%@"&lt;&gt;\(\)\+,/g, "");
  </code></pre>
  
  <p>I am escaping the regex special chars with a backslash but I am having a hard time trying to understand what's going on. </p>
  
  <p>If I try with single literals in isolation <em>most of them</em> seem to work, but once I put them together in the same regex depending on the order the replace is broken.</p>
  
  <p>i.e. this won't work --> <code>dirtyString.replace(/\|&lt;&gt;/g, ""):</code></p>
  
  <p>Help appreciated!</p>