I have a string example
"abc|pqr[abc,xyz[abc,def]]"
Now i want to output into array
{
abc : true,
pqr : ['abc', xyz : [abc, def]]
}
the code i wrote is this but it give me
"message": "Maximum call stack size exceeded"
var x = 'p[a,b,c,d]|q[small,large]|r[small,large]|s|t[w[x,y],z[a,b,c]]';
y = x.split("|");
function foo(query) {
if (typeof query == "string") query = [query]
var i = {}
_(query).forEach(function(v) {
regexQuery = v.match(/\[(.*)\]/);
if (regexQuery != null) {
index = regexQuery['index']
if (regexQuery[1].match(/\[(.*)\]/) != null) {
i[regexQuery['input'].substr(0, index)] = foo(regexQuery[0])
} else {
i[regexQuery['input'].substr(0, index)] = regexQuery[1].split(",");
}
} else {
i[v] = true;
}
})
return i;
}
console.log(foo(y));
i know regex is not got for this but is there any other solution?
x. btw, why is a:necessary and why is it not in the first stringabc|pqr[abc,xyz[abc,def]]?xyz: [abc, def]as one of the elements in the array. Did you mean to wrap that in{}and forget to put quotes around"abc"and"def"?