You could do this in a two step regex if you so desire:
[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*{(.*)}
Your odd matches are your keys, and your even matches are your values. You'd need to run a secondary regex on your even matches to find all your values, something like:
[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*(?:true|false)
Which should extract all your values.
EDIT:
You cannot accomplish this with only a single regex and here's why, if you capture each variable in quotes you'll end up with 6 captures:
- cccccc
- aaaaaa
- xxxxxxxx
- bbbbbbb
- yyyyy
- zzzzzzzz
It's completely unclear what any of these are without a visual inspection of the input. The only way to define a variable as contained by another variable is to extract only the variable name then only the contents therein the meter gives defines what the capture is. My first capture would give you:
- cccccc
- aaaaaa
- "xxxxxxxx": true
- bbbbbbb
- "yyyyy": true, "zzzzzzzz": true
JavaStript does not currently support repetitive captures. But even if it did think about the complexity of combining my two regexes into one:
[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*{((?:[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*{(.*)})*)}
You'd end up with the following matches:
- cccccc
- aaaaaa
- "xxxxxxxx": true
- xxxxxxxx
- bbbbbbb
- "yyyyy": true, "zzzzzzzz": true
- yyyyy
- zzzzzzzz
You'd have to process the whole set of captures, identifying captures as contents. You could do that by finding any empty capture or any capture containing a '"'
character. Then you could assume that every variable immediately preceding a capture of contents was a containing variable, and every variable succeeding a capture of contents was a contained variable; up to the next containing variable.
Of course aside from creating a lot of extra work, as I linked, this isn't currently possible in JavaScript regexes. So I'll return to my original answer's statement: As Bergi mentioned avoidance of regexes in favor of a programatic solution, JSON.parse
or similar, should always be preferred because of the fragility of regexes.