I'm trying to split a string by space but ignore those in parentheses, or after an opening parenthesis. I followed this solution but my case is a little bit more complicated. For example if the parentheses are balanced, that solution works fine:
// original string
let string = 'attribute1 in (a, b, c) attribute2 in (d, e)';
words = string.split(/(?!\(.*)\s(?![^(]*?\))/g);
console.log(words)
expected result after split:
words = ['attribute1', 'in', '(a, b, c)', 'attribute2', 'in', '(d, e)']
However if the parentheses are not balanced, let's say:
// original string
let string = 'attribute1 in (a, b, c) attribute2 in (d, e';
Then the result I expected should be:
['attribute1', 'in', '(a, b, c)', 'attribute2', 'in', '(d, e']
instead of
['attribute1', 'in', '(a, b, c)', 'attribute2', 'in', '(d,', 'e']
How should I achieve this?