I am trying to pick out all values from an object I have in string form. I have created the regular expression, but I am still having issues with not being able to remove the quotes and have hit a wall...
Here is the code I have with results I get compared to desired results:
const regex = /(?:"([^"]+)\")|([^=",{}.]+)/g
const string = 'obj{a="0",b="1",domain="a-ss.test.io:666",f="g",range="3.594e-04...4.084e-04"}'
const matches = string.match(regex)
console.log(matches)
Here is the resulting array:
[
"obj",
"a",
"\"0\"",
"b",
"\"1\"",
"domain",
"\"a-ss.test.io:666\"",
"f",
"\"g\"",
"range",
"\"3.594e-04...4.084e-04\""
]
Though the desired result I would like is:
[
"obj",
"a",
"0",
"b",
"1",
"domain",
"a-ss.test.io:666",
"f",
"g",
"range",
"3.594e-04...4.084e-04"
]
Does anyone know how to also remove the quotes from each array value that is returned?
You need to get whole match values in the result since
String#matchwith a regular expression containing the/gflag loses all captures. See thatString#matchreturns:You want to get Group 1 or Group 2 values, so ask the engine to return just them: