Reputation: 1893
As I am trying to improve my regular expression skills, I am trying to solve this use case.
Let's say a server delivers an incorrectly formatted JSON string and we want to convert this string so that we can convert it to JSON:
let x = '{"my_text":"hello "world"","test2":"tree","blue":"red "yellow","orange":"green, purple "cyan""}';
I have a regex that matches all double quotes inside double quotes, except for the ones around cyan
.
/(?<=\:\"([^\,\"]*))\"(?=([^\,]*)\"\,)/g
If I am correct, it is because [^\:\"]
excludes either :
or "
, while I want to exclude them sequentially (:"
) from the positive lookbehind.
Now I've thought about doing a negative lookbehind after the positive lookbehind, but it's not matching and I'm not sure why:
/(?<=\:\")(?<!(.*)\:\"(.*))\"(?=([^\,]*)\"\,?)/g`
Am I missing something? How do I match all double quotes inside double quoted values?
Upvotes: -1
Views: 64
Reputation: 1893
I ended up solving my own question - despite comments.
let x = '{"my_text":"hello "world"","test2":"tree","blue":"red "yellow","orange":"green, purple "cyan""}';
x = x.replaceAll( /(?<![\,\:\{])\"(?![\:\,\}])/g, '\\"' );
const j = JSON.parse( x );
console.log('j:', j)
The regex pattern is inverted to exclude the double quotes that should be there.
This may not be a perfect solution as others pointed out, due to its vulnerabilities for other potential issues being delivered from the server. But at least it is solving the issue in question.
In the mean time I am waiting for a response from the third party serving this API output and see if they are able to solve it on their end.
Result
Upvotes: 0