Skip to content

Instantly share code, notes, and snippets.

@j03m
Created December 8, 2014 15:23
Show Gist options
  • Save j03m/2022cf4c82305dec26a7 to your computer and use it in GitHub Desktop.
Save j03m/2022cf4c82305dec26a7 to your computer and use it in GitHub Desktop.
find matching curly brackets - takes into account that it might hit strings yay (from dev_tools chrome source)
findBalancedCurlyBrackets: function(source, startIndex, lastIndex) {
lastIndex = lastIndex || source.length;
startIndex = startIndex || 0;
var counter = 0;
var inString = false;
for (var index = startIndex; index < lastIndex; ++index) {
var character = source[index];
if (inString) {
if (character === "\\")
++index;
else if (character === "\"")
inString = false;
} else {
if (character === "\"")
inString = true;
else if (character === "{")
++counter;
else if (character === "}") {
if (--counter === 0)
return index + 1;
}
}
}
return -1;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment