Here is a sample string:
key[type=order][0].item[id={getVal(obj[type=item][0].id)}]
I am looking to split this string into the following format:
[
['key', '[type=order]', '[0]'],
['item', '[id={getVal(obj[type=item][0].id)}]']
]
Initially, I divide the string by the first level dot:
const str = 'key[type=order][0].item[id={getVal(obj[type=item][0].id)}]';
const arr = str.split(/\.(?![^[]*])/).filter(Boolean);
This results in an array like so:
['key[type=order][0]', 'item[id={getVal(obj[type=item][0].id)}]']
Next, I aim to split each item of the array using the first level square brackets:
arr.map(item => item.split(/(\[.+?\])/).filter(Boolean))
Which produces an array as follows:
[
['key', '[type=order]', '[0]'],
['item', '[id={getVal(obj[type=item]', '[0]', '.id)}]']
]
I intend to split the items only based on the first level square brackets without delving into nested ones. How can I achieve this using regex in JavaScript?