我想通过空格来拆分一个字符串,但要忽略括号内的空格和括号后面的空格。我参考了 这个解决方案,但我的情况稍微有点复杂。例如,如果括号是平衡的,那么该解决方案就能很好地工作:
// original string
let string = 'attribute1 in (a, b, c) attribute2 in (d, e)';
words = string.split(/(?!\(.*)\s(?![^(]*?\))/g);
console.log(words)
拆分后的预期结果:
words = ['attribute1', 'in', '(a, b, c)', 'attribute2', 'in', '(d, e)']
不过,如果括号没有平衡,举个例子:
// original string
let string = 'attribute1 in (a, b, c) attribute2 in (d, e';
我期望的结果应该是:
['attribute1', 'in', '(a, b, c)', 'attribute2', 'in', '(d, e']
替代
['attribute1', 'in', '(a, b, c)', 'attribute2', 'in', '(d,', 'e']
我该如何实现这个?