【发布时间】:2019-12-19 12:44:28
【问题描述】:
我有一个这样的字符串
const str = "'a' 'b' 'c'"
我想拆分它并得到字符串的结果数组
const arr = str.split(" ")
但在输出中我得到:
["'a'", "'b'", "'c'"]
如何在没有嵌套字符串的情况下进入输出数组?
想要的结果
["a", "b", "c"]
【问题讨论】:
-
这些字符串中的一个是否有可能转义
'字符?例如["'doesn\'t' 'abc' 'mustn\'t'"] -
Array.from(text.matchAll(/'([^']+)'/g), m => m[1]) -
或
Array.from(text.matchAll(/'([^'\\]*(?:\\.[^'\\]*)*)'/gs), m => m[1])
标签: javascript arrays regex split