我希望能够使用split()
方法将任何字符串通过空格分割成数组。但是,我希望忽略引号中的空格。
例如:
'word "words in double quotes"'
它应该变成一个数组,其中包含:
[
'word',
'words in double quotes'
]
我看了一些类似的答案,它们通常会给出一个数组:
[
'word',
'"words in double quotes"'
]
而这不是我要找的。我不想将引号添加到数组元素中。
我可以使用什么正则表达式?
split
是不可能的,因为split
无法移除最后一个引号,除非再引入另一个split
。你可以尝试使用 `const [match, first, second] = string.match(/^(.) "(.)"$/)。 - le_m'word "words in double quotes"'.split(/"(.*?)"|\s+/g).filter(Boolean)
,但取决于引号内的引号如何转义。 - Slai