Created
June 25, 2014 13:36
-
-
Save luelista/eba639ee62900ab41193 to your computer and use it in GitHub Desktop.
Parser for matching parentheses and strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function check_parens(str) { | |
var br=0,skip=false,instr=false; | |
for(var i = 0, len = str.length; i<len; i++) { | |
if(skip) { skip=false; continue; } | |
var z = str[i]; | |
switch(z) { | |
case "[":case "{":case "(": if(!instr)br++;break; | |
case "]":case "}":case ")": if(!instr)br--; if(br==0)return i; break; | |
case "\"":case "'": instr=!instr; | |
case "\\": skip=true; break; | |
} | |
if (br<0) return false; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment