Javascript匹配所有正则表达式,该正则表达式返回用双引号括起来的所有短语的数组

Javascript匹配所有正则表达式,该正则表达式返回用双引号括起来的所有短语的数组,javascript,arrays,regex,string,Javascript,Arrays,Regex,String,我有一个类似“这个和那个”的字符串。我想返回所有双引号字符串的数组,即[这个,那个] 到目前为止,我已经尝试: var mystring = '"this one" and "that one"'; var m = mystring.match(/"(.*?)"/); alert(m[1]); 它可以很好地检测双引号字符串的首次出现,但是如何将所有短语/单词用双引号括起来?我认为您缺少全局修饰符: var mystring = '"this one" and "that one"'; var

我有一个类似“这个和那个”的字符串。我想返回所有双引号字符串的数组,即[这个,那个]

到目前为止,我已经尝试:

var mystring = '"this one" and "that one"';
var m = mystring.match(/"(.*?)"/);
alert(m[1]);

它可以很好地检测双引号字符串的首次出现,但是如何将所有短语/单词用双引号括起来?

我认为您缺少全局修饰符:

var mystring = '"this one" and "that one"';
var m = mystring.match(/"(.*?)"/g);
console.log(m);
更新

var mystring = '"this one" and "that one"';
var m = mystring.match(/"(.*?)"/g).map(function(n){ return n.replace(/"/g,'')});
console.log(m);

我认为您缺少全局修改器:

var mystring = '"this one" and "that one"';
var m = mystring.match(/"(.*?)"/g);
console.log(m);
更新

var mystring = '"this one" and "that one"';
var m = mystring.match(/"(.*?)"/g).map(function(n){ return n.replace(/"/g,'')});
console.log(m);

这很接近。它正在返回[this one,that one],但我希望[this one,that one]不带双引号。在这种情况下,您可以简单地去掉引号:我知道我在问题中没有要求这样做,但是如果字符串不包含一对双引号,则会将未捕获的TypeError:Cannot read属性“map”的null发送到控制台。我用if-statement解决了这个问题,很接近了。它正在返回[this one,that one],但我希望[this one,that one]不带双引号。在这种情况下,您可以简单地去掉引号:我知道我在问题中没有要求这样做,但是如果字符串不包含一对双引号,则会将未捕获的TypeError:Cannot read属性“map”的null发送到控制台。我用if语句修复了这个问题。