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,
|
and they wanted to split the string at all the commas, except when they are preceded by a backslash. That means, they wanted
|
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
|
Since JavaScript doesn’t have lookbehinds, we don’t have a straight forward solution to this problem. When I searched Stackoverflow, I found this solution
|
This solves the exact same problem which my colleague was facing. It compensates the absence of lookbehinds by breaking down the problem in two steps.
- Replace all the commas matching the condition with a marker string which does not appear anywhere in the string. (
\u000B
in this case) - Then split the original string with the marker string.