I don't know if this has been asked before, because English is not my first language and I don't know the keywords to search.
So basically I have the following input element,
<input type="email" name="person[0].email" />
I would like to split the name into 3 parts like ["person", "0", "email"]
.
I have tried using /(\[[^[\]]])|\./
but it gives ["person", "[0]", "", undefined, "email"]
.
Also, for a[0][1].b[3].c
, it should output ["a", "0", "1", "b", "3", "c"]
You can use .match
instead of .split
console.log("person[0].email".match(/\w+/g));
Note (thanks @npinti
): in case if in name will be _
my first example will match also _
, so in this case you need just use regexp like this
console.log("person[0].email".match(/[A-Za-z0-9]+/g));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With