I need to write a regex to target commas that exist only outside a pair of brackets or braces.
Currently I have:
var regex = /,(?![^{]*})(?![^[]*])/g
When the target string is:
var str = '"a":[{"b":2,"c":["d"]}],"b":2' // OK: only second comma matches
the pattern correctly matches only the second comma.
When the target string is:
var str = '"a":[{"b":2,"c":{"d":9}}],"b":2' // OK: only second comma matches
the pattern also correctly matches only the second comma.
However, when the target string includes a array and object, the negative lookahead fails and the regex matches both commas.
var str = '"a":[{"b":2,"c":[{"d":9}]}],"b":2' // BAD: both commas match