JavaScript的比赛,返回双引号括起来的所有短语阵列中所有的正则表达式 [英] Javascript match all regex that returns an array of all phrases wrapped in double quotes

查看:98
本文介绍了JavaScript的比赛,返回双引号括起来的所有短语阵列中所有的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串像'这一个和一个'。我想回到所有双引号中的字符串即数组[这一个,那一个]

I have a string something like '"this one" and "that one"'. I want to return an array of all double quoted strings i.e. [this one, that one].

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

So far I've tried:

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

它工作正常,在检测到双引号的字符串的第一次出现,但如何让所有的双引号括起来的短语/词?

It works fine at detecting the first occurrence of a double quoted string, but how do I get all of the phrases / words wrapped in double quotes?

推荐答案

我认为你缺少的(G)叶形修正:

I think you are missing the (g)lobal modifier:

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);

这篇关于JavaScript的比赛,返回双引号括起来的所有短语阵列中所有的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆