I have a string that I need to convert into an array of objects
const str = "Nike (brand|category) shoes(product) for women(o)"
Expected result
let output = [
{
key:"Nike",
value:["brand","category"]
},
{
key:"shoes",
value:["product"]
},
{
key:"for women",
value:[]
}
]
Here is the code I attempted
function generateSchema(val) {
let split = val.split(" ")
let maps = split.map((i) => {
let obj = i.split("(")
let key = obj[0].replaceAll(/\s/g, "")
let cleanValue = obj[1].replace(/[{()}]/g, "")
let stripedValues = cleanValue.split("|")
return {
key: key,
value: stripedValues,
}
})
return maps
}
let out = generateSchema(str)
However, this breaks when there is a word with a space, for example for women
How can I split with a regex?