title: Check if string "is" javascript code with regex
body: <p>For a very simple javascript template engine I need to check whether a given string <em>"is"</em> javascript code (like <code>if-else</code>,<code>for-loops</code> or <code>while-loops</code>). Therefore I'm using this very simple regex <code>/(:|=|{|})/g</code> (<a href="http://regexr.com/39l8q" rel="nofollow">"tested" here</a>). While this regex works almost anytime it somehow fails in some cases when I'm using it in a loop:</p>
  
  <pre><code>// Very simple regex to check if string "is" javascript code
  var regFunc = /(:|=|{|})+/g;
  // For testing: a simple javascript array
  var testArray = [
    // expected: false, is: false
    'string',
    // expected: true, is: true
    'for(var i=0; i&lt;total;i++) {',
    // expected: true, is: false (??)
    '}',
    // expected: true, is: true
    '}'
  ];
  
  for( var i = 0; i &lt; testArray.length; i++ ) {
    console.log(
      testArray[ i ],
      regFunc.test( testArray[ i ] )
    );
  }
  </code></pre>
  
  <p>You can check the console output on <a href="http://jsbin.com/guwaleyipuyu/1/edit" rel="nofollow">JSBin</a>. So I'm wondering why the first "{" outputs <code>false</code> and the second one outputs <code>true</code> (which is what I expect for both)?</p>