我正在使用以下正则表达式:
假设值为Matthew Smith (Is a Builder)
this.value.match(/(\([^\)]+\)|\S+)/g);
此返回[Matthew],[Smith], [(Is a Builder)].
我想返回[Matthew],[" "],[Smith],[" "],[(Is a Builder)].
任何身体可以帮忙吗?
答案 0 :(得分:2)
同时将\s+
添加到替换中。
/(\([^\)]+\)|\S+|\s+)/
答案 1 :(得分:0)
试试这个:
/(\(.*\)|\S+|\s+)/
\(.*\)
匹配表单(any character (even nested brackets) inside)
\S+
匹配任何非空格字符串
\s+
匹配任何字符串空格字符
Matthew Smith (Is a Builder)
Matthew Smith (Is a (very good) Builder)
[Matthew], [" "], [Smith], [" "], [(Is a Builder)]
[Matthew], [" "], [Smith], [" "], [(Is a (very good) Builder)]