title: JavaScript RegExp test method works not stable , I mean the result is different between first execute and the second time
body: <p>Just like title says , code behind like</p>
  
  <pre><code>var re = /^\S(if\([\s\S]*?\)\s*?\{)([\s\S]*?)(\})(?:\s*?(else\s*?\{)([\s\S]*?)(\}))?/g;
  var str = "@if(bool) { @{code} &lt;div&gt;@(var)&lt;/div&gt;} else {  @while(condition){ @{ } &lt;div&gt;@(var)&lt;/div&gt;} &lt;div&gt;str&lt;/div&gt; }"
  
  //first time execute
  re.test(str);    //true
  
  //second time execute
  re.test(str);    //false
  
  //third time execute
  re.test(str);   //true
  </code></pre>
  
  <p>The even times I execute result is true , the odd times I execute is false . </p>
  
  <p>The Situation Is I am building a front-end template engine , the razor style , I want it can parse code template like this</p>
  
  <pre><code>@if(some condition)
  {
      &lt;div&gt;@(some variable or sentence code)&lt;/div&gt;
      @for(var i = 0; i &lt; 10 ; i ++)
      {
          &lt;div&gt;@(i)&lt;/div&gt;
      }
  }
  else
  {
      &lt;div&gt;bla bla ...&lt;/div&gt;
  }
  </code></pre>
  
  <p>So I need to use Regex to fetch the template part , but I account this problem now , looks weird ... any help or guide ,thanks ...</p>