I need to parse a string that's a comma-separated lists of parameters of the form
key1=value1,key2=value2,key3=value3...
The complication is that the values can be enclosed in quotation marks to allow them to contain spaces and commas and such. Of course the quotation-mark enclosed commas should not count as separating parameters. (There can also be spaces in various places outside the quotation marks that maybe should be ignored.)
My thought is to split the list at the commas, and then inside each parameter definition, to separate the key from the value at the equal sign. So to split
the parameters, I need to find the valid ones (not in quotation marks); I'm thinking a regular expression is the way to go for brevity and directness.
Here are some example strings:
Include="All Violations", CheckType=MaxTrans
MetricName = PlacedInstances, PlacedOnly = 1
CheckType=Hold, Include="reg2reg,in2reg,in2out,reg2out"
CheckType=Setup, Include="reg2reg,in2reg,in2out,reg2out
(sic)
Yes, the last one is poorly formed: missing a terminating quotation mark in the value.
I found this answer helpful (regex: /,(?=(?:(?:[^"]*"){2})*[^"]*$)/
), except for parsing the poorly formed one. In my case I've got additional information in the equal sign, which would allow parsing that one.
I tried this: (/(?<==[^"]+),/
, which works for the poorly formed one, but fails my first example. I think what I need is a way to find commas preceded by an equal sign but which have either zero or two quotation marks (not just a single quotation mark) between them and the first preceding equal sign. But how do I write that in Javascript Regex?