title: Regular Expression - new RegExp() searching/replacing string globally
body: <p>I'm working on a project which at the moment is unable to have any 3rd party JS libraries (otherwise this would be a breeze with jQuery).  One of the things within this project is form validation.  I've got that working fairly well except for some minor issues with my regex.</p>
  
  <p>I'm triggering a validation function onchange() of text fields so if you tab out and there's an error you'll know immediately.  Tab out with good data and you'll know immediately as well.  If the validation fails, I assign an <strong>error</strong> class to a parent element.  This works fine if you only trip the validation once.  If you keep failing the test, the error classes build up, for example, if you fail the validation 2x, you'll end up with:</p>
  
  <pre><code>&lt;p class=" error"&gt;
  </code></pre>
  
  <p>If you fail 3x, you'll end up with:</p>
  
  <pre><code>&lt;p class=" error error"&gt;
  </code></pre>
  
  <p>If the validation passes, I call the removeClass() function below.  Basically, it doesn't look like I'm getting a global search/replace.  I thought for sure that adding the global modifier would take care of the case above, but it does not, it only removes one "error" class.</p>
  
  <pre><code>removeClass: function (el, name) {
      var regex = new RegExp('(^|\\s)' + name + '(\\s|$)', 'gi');
      el.className = el.className.replace(regex, ' ');
  }
  </code></pre>
  
  <p>My regex is just looking for beginning of line or white space followed by "error" followed by white space or end of line.  Granted, I admittedly suck at regular expressions :)</p>
  
  <p>Anything jump out at you?</p>