0
votes

I have a string that resembles this:

let string = "'arnold', 28, 'software developer', '{"lat":0.31628,"lng":"32.58219"}'"

I would like to split the string by commas and end up with something like this:

['arnold', 28, 'software developer', '{"lat":0.31628,"lng":"32.58219"}']

Reason is, I am working on a legacy system and I want to be able to receive string like that and prepare them for inserts. Is this possible with JavaScript/NodeJS? I have tried using string.split(',') but I get an undesirable result, i.e it splits the string in the braces. I will appreciate any help in figuring it out.

I got the feeling there must be a duplicate around on SO. For now, maybe it's enough to check for the presence of closing brackets before an opening one? ,(?![^{]*})\s* - JvdV
I would use a CSV library because I have real work to do. A simplistic CSV grammar isn't terribly difficult, but it's annoying to get it consistently correct. - Dave Newton