Skip to content

Instantly share code, notes, and snippets.

@luelista
Created June 25, 2014 13:36
Show Gist options
  • Save luelista/eba639ee62900ab41193 to your computer and use it in GitHub Desktop.
Save luelista/eba639ee62900ab41193 to your computer and use it in GitHub Desktop.
Parser for matching parentheses and strings
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