如何仅通过引号拆分给定的文本?例如:
'He said "This is true", then added "Lets go"'
会像这样拆分:
['He said', '"This is true"', ', then added', '"Lets go"']
感谢您的帮助。
最佳答案
您可以将匹配
与基于交替的正则表达式一起使用:
const str = 'He said "This is true", then added "Lets go"'
var arr = str.match(/"[^"]*"|[^"]+/g);
console.log(arr);
正则表达式有两种选择:
[^"]*"
:匹配引号字符串|
:或者[^"]+
:匹配1+个非双引号字符
关于javascript - 用引号分割文本而不是javascript中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54924244/