title: Javascript Regular Expression is working fine first time but not second time, working again for 3rd time and not for 4th and so on
body: <p>Javascript Regular Expression is working fine first time but not second time, working again for 3rd time and not for 4th  and so on :(</p>
  
  <p>Script : </p>
  
  <pre><code>&lt;script language="javascript" type="text/javascript"&gt;
      var reg = /[^\w]/gi;
      function checkNonWordChars() {
          var str = $("#TestTextbox").val();
          if (reg.test(str)) {
              alert('!!! Non-Word Char Exists !!!');
          }
          else {
              alert('input accepted');
          }
      }    
  &lt;/script&gt;
  </code></pre>
  
  <p>HTML :</p>
  
  <pre><code>&lt;input type="text" id="TestTextbox"  /&gt;
  &lt;input type="button" value="Test" onclick="checkNonWordChars();" /&gt;
  </code></pre>
  
  <p>If I click on button once, it will fire an alert saying that  “!!! Non-Word Char Exists !!!” but if I click it again, it will fire an alert saying “input accepted” :(</p>