i search for a regex to split the following string:
aaa[bbb,ccc[ddd,{eee:1,mmm:999}],nnn[0,3]]
aaa[bbb,ccc[ddd,{eee:1, mmm:[123,555]}],nnn[0,3]]
aaa[bbb, ccc[ddd, ddd],nnn[0,3]]
aaa[bbb,ddd[0,3]]
by ‘[‘ or ‘]’ or ‘,’ unless the ‘,’ is in ‘{}’. As example: split ‘aaa[bbb,ccc[ddd,’ to aaa, bbb, ccc, ddd is allow but not {eee:1,mmm:999}.
the result:
aaa, bbb, ccc, ddd, {eee:1,mmm:999}, nnn, 0, 3
aaa, bbb, ccc, ddd, {eee:1, mmm:[123,555]}], nnn, 0, 3
aaa, bbb, ccc, ddd, ddd, nnn, 0, 3
aaa, bbb, ddd, 0, 3
i have read meany other questions but i cant modifie the regex’s there are post to do this what i want.
the target language for the expression is javascript.
Perl/PCRE regex, should work in JS too (as long as {} aren’t nested):
Output:
A rough translation into JavaScript:
It’s not clear what the line breaks in the input or result data in the question are meant to be. In the above, they’re line breaks in the input data and then not treated specially in the result. If they need to be treated specially, the OP can edit appropriately. And so this is the result of the above (again, using
<<and>>as separators rather than,for clarity around whether{...}gets processed):