Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将字符串除以空格,但前提是空格不在方括号内。例如:
“多么美好的晴天啊”
分割后应该是这样的:
[什么,一个,[晴天],一天]
我试图自己找到解决方案,但恐怕我对 javascript 中的 RegExp 还不够熟悉。
当必须包含空格时,需要以下 RegEx:
"What a [nice sunny] day".match(/(^|\s)(\[[^\]]+\]|\S+)/g) // Outputs: ["What"," a"," [nice sunny]"," day"]
在评论中,很明显必须删除空格:
"What a [nice sunny] day".match(/\[[^\]]+\]|\S+/g) // Outputs: ["What","a","[nice sunny]","day"]