Thinking about it again, I'd make a slight change. Instead of chopping it up first, I'd use a search for brace characters with /g as the condition of a while loop, so the body can examine pos() and push and pop and stuff, and all the uninteresting characters are skipped over.