Reputation: 1076
I am trying to get code between two braces, but still pay attention to nesting. Say I have something like the below as input:
while (true) { [A]
dothis();
if (whattype() == "A") { [B]
doA();
if (other() == "dog") { [C]
doB();
} [D]
} [E]
if (other() == "cat") { [F]
doZ();
} [G]
} [H]
And I want to recursively loop each nesting layer:
while
- if
- if
- if
The current function takes the string, uses regex (\{([\s\S]*)\}
) to greedily find code between the first and final brace and does that again to its contents until there are no more braces in the string.
The problem is the regex does not work for blocks of code next to each other. The regex matches the text between B up until G. It should instead start at B and stop at E, then another block from F to G.
Edit: I may end up using something other than regex. Are there any suggestions on how to handle this?
What I found helpful was this answer from another SO question.
Upvotes: 0
Views: 98
Reputation: 4706
This type of problem can't be resolved by a regex that consume a whole block.
What you describe does require complete and correct tokenization of the JavaScript language. Consider for example that you might have brackets contained inside quoted text... Unless you actually see more benefits I'm trying to do it all by yourself than to actually succeed in reasonable time (like you are playing around to understand how parsers works), then you should definitely have a look at some existing JS in JS parsers. See for example: http://marijnhaverbeke.nl/blog/acorn.html (note this is the first result that Google gave me, never tried that library).
Upvotes: 3