2021-06-28

Replace single quotes, but not if within double quotes to use JSON.parse()

I need a solution to only exchange pairs of single quotes, which are NOT within a pair of double quotes.

A simplified example (original is > 100 KB):

['Element1',"Element's2",{"Element3":"E3"},'Element4']

In this example Element's2 should not become Element"s2, but 'Element1' should become "Element1".

Background: I need to extract some website data contained in big Javascript blocks. To get it i need JOSN.parse() which does not accept single quotes (') as quotes.

but i can not simply use replace(/'/g, '"'), because the single quote is also used as apostrophe in some texts like: "that's it".

BTW: As @hakre recommenced, a much more simple solution for my case was to use eval() instead of JOSN.parse(). With eval() there was no need to exchange the single quotes.



from Recent Questions - Stack Overflow https://ift.tt/3jlpR5Q
https://ift.tt/eA8V8J

No comments:

Post a Comment