I have a form with input fields like this:
lines[1][config][options][346]
I can get the last part with this:
var name = 'lines[0][config][options][343]';
let ido = name.lastIndexOf("["),
idc = name.lastIndexOf("]"),
index = name.substring(ido + 1, idc);
console.log(index);
But what if i am interested in first, second, third etc. name?
3
Answers
Flatten the data structure into a single dimension array. First hack the string up then trim the fat:
js
regular expression
solutionUse a simple regular expression:
here’s explained on Regex101.com, basically:
(?<=[)
[
[^]]+
]
one or more times(?=])
]
g