If I have a string formatted as follows:
"name", "bob", "number", 16, "place", "somewhere"
And instead, I want to have a line like this:
"name": "bob", "number": 16, "place": "somewhere"
In addition, in the test examples there are several examples of such lines:
"name", "bob", "hello, world", true
This needs to be formatted as follows:
"name" : "bob", "hello, world" : true
... when replacing each odd comma with a colon (as long as this comma goes beyond the quotes) , how on earth will I do this through regular expression?
I found the following regex via Google: /(,)(?=(?:[^"]|"[^"]*")*$)/,':' Which matches the first instance of the comma. How can I alternate each other?
Edit for more information:
What I'm trying to do is take this line where each value is defined by a comma and formats it as a JS object using .replace() . Thus, in this case, βnameβ, βnumberβ and βplaceβ are key values. They are not fixed, this is just an example.