Created
December 8, 2014 15:23
-
-
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)
This file contains 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
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