我有这样的命令:add "first item" and subtract "third course", disregard "the final power".
我如何提取所有的字符串,因此输出数组:["first item", "third course", "the final power"]
尝试使用匹配quote
,text
,quote
正则表达式,然后使用map
删除所拍摄的报价:
const string = 'add "first item" and subtract "third course", disregard "the final power".';
const quotes = string.match(/\"(.*?)\"/g).map(e => e.split("\"")[1]);
console.log(quotes);
一种解决方案是使用这样的全局RegExp,只是环通
var extractValues = function(string) {
var regex = /"([^"]+)"/g;
var ret = [];
for (var result = regex.exec(string);
result != null;
result = regex.exec(string)) {
ret.push(result[1]);
}
return ret;
}
extractValues('add "first item" and subtract "third course", disregard "the final power".')
但是请注意,如此,大部分的答案,包括这一个,不处理的事实,值可能在他们的报价。因此,例如:
var str = 'This is "a \"quoted string\""';
如果你有这样的数据集中的,你需要适应一些答案。
您可以使用此
“[^”] +“? - 匹配的POI随后除qazxsw POI(一个或多个时间懒惰模式)的任何qazxsw随后"
"
"
参考:let str = `add "first item" and subtract "third course", disregard "the final power"`
let op = str.match(/"[^"]+?"/g).map(e=>e.replace(/\"/g, ''))
console.log(op)