gpt4 book ai didi

javascript - 用引号分割文本而不是javascript中的空格

转载 作者:行者123 更新时间:2023-11-30 14:08:24 25 4
gpt4 key购买 nike

如何仅通过引号拆分给定的文本?例如:

'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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com