Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split person[0].email into ['person', '0', 'email']

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"]

like image 719
Lucius Avatar asked Dec 07 '15 08:12

Lucius


1 Answers

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));
like image 124
Oleksandr T. Avatar answered Oct 19 '22 23:10

Oleksandr T.