Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RegExp以查找数组中引用的单词的所有出现_Javascript_Regex - Fatal编程技术网

Javascript RegExp以查找数组中引用的单词的所有出现

Javascript RegExp以查找数组中引用的单词的所有出现,javascript,regex,Javascript,Regex,给我一句话: var testsentence = 'This "is" a wonderful "sentence" to "test" "stuff"'; 我怎样才能得到这样的数组 var testarray = [ "is", "sentence", "test", "stuff" ] 更新 我正在使用Chromium控制台尝试您的响应,但到目前为止,所有响应都返回: [""is"", ""sentence"", ""test"", ""stuff""] 我不想在比赛中使用引号 (t

给我一句话:

var testsentence = 'This "is" a wonderful "sentence" to "test" "stuff"';
我怎样才能得到这样的数组

var testarray = [ "is", "sentence", "test", "stuff" ]
更新

我正在使用Chromium控制台尝试您的响应,但到目前为止,所有响应都返回:

[""is"", ""sentence"", ""test"", ""stuff""]
我不想在比赛中使用引号

(testsentence.match(/"\w+"/g) || []).map(function(w) {
    return w.slice(1, -1);
});
应该返回所有引用的内容,并避免类似


要捕获带引号的文本,而不是引号,请注意match不会返回带有g修饰符的组,因此请使用以下内容循环匹配:

var testsentence = 'This "is" a wonderful "sentence" to "test" "stuff"';
var pattern = /"([^"]+)"/g;
var match;
var testarray = [];
while(match = pattern.exec(testsentence)) {
    testarray.push(match[1]);
}

只匹配引用的单词。他说,“好吧,我不会突然这么做,因为我不会匹配的。”克莱门瑟雷曼:应该匹配吗?我不这么认为。对于这样的解决方案,请参阅burning_LEGION和RicardoLohmann的答案。但我会在匹配中使用引号。@user1680104:几乎所有匹配的解决方案都会使用引号。只需从结果中删除它们。谢谢。所以真的没有更简单的吗?如果我以前有一个类似的非静态模式的任务呢?像/sequencepart\d+stufftofind/但是它在数组中有引号这真的是最快的方法吗?希望有更简单的方法。不确定……如果不在var模式中保存regex,那么它就不会在每个循环中迭代匹配。如果您不介意保留引号,那么其他人的解决方案会更简单。顺便说一句,不需要在字符类周围使用分组运算符
return testsentence.match(/".+?"/g);
testsentence.match(/"[^"]+"/g);
var testsentence = 'This "is" a wonderful "sentence" to "test" "stuff"';
var pattern = /"([^"]+)"/g;
var match;
var testarray = [];
while(match = pattern.exec(testsentence)) {
    testarray.push(match[1]);
}