opinions are my own; try code/suggestions at your own risk

JavaScript Lookbehind RegEx Alternative

Recently one of my colleagues came to me with a problem. He had a string which looked like JSON but not a valid JSON string. For example,

[{\"name\":\"Kilua\"\\,\"age\":35}],{\"city\":\"SJC\"},\"US\"

and they wanted to split the string at all the commas, except when they are preceded by a backslash. That means, they wanted

['[{"name":"Kilua"\\,"age":35}]','{"city":"SJC"},"US"']

as output. If the requirement was to split the string at commas followed by a certain string, then it would have been pretty straight forward. For example, if the string is a,1,b,2,c,3 and if they wanted to split the string at all commas except when they are not followed by numbers, then the solution would have been very simple, with negative lookahead, like this