Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.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
Rubular与javascript正则表达式捕获组_Javascript_Regex_Rubular - Fatal编程技术网

Rubular与javascript正则表达式捕获组

Rubular与javascript正则表达式捕获组,javascript,regex,rubular,Javascript,Regex,Rubular,注意到Rubular.com和Javascript正则表达式之间的差异: 'catdogdogcatdog'.match(/cat(dog)/g); // JS returns ['catdog', 'catdog'] 我本想抓两次“狗”,但我却抓了两次“猫狗” Rubular捕捉到的“狗”是预期的两倍: 这到底是怎么回事?不,Rubular还匹配了两次猫狗。它还向您显示捕获组的内容,该组捕获了dog两次 你想要这样的东西: var myregexp = /cat(dog)/g; va

注意到Rubular.com和Javascript正则表达式之间的差异:

'catdogdogcatdog'.match(/cat(dog)/g);  // JS returns ['catdog', 'catdog']  
我本想抓两次“狗”,但我却抓了两次“猫狗”

Rubular捕捉到的“狗”是预期的两倍:


这到底是怎么回事?

不,Rubular还匹配了两次猫狗。它还向您显示捕获组的内容,该组捕获了
dog
两次

你想要这样的东西:

var myregexp = /cat(dog)/g;
var match = myregexp.exec(subject);
while (match != null) {
    dog = match[1]
    // do something, Gromit!
    match = myregexp.exec(subject);
}

不,Rubular也匹配猫狗两次。它还向您显示捕获组的内容,该组捕获了
dog
两次

你想要这样的东西:

var myregexp = /cat(dog)/g;
var match = myregexp.exec(subject);
while (match != null) {
    dog = match[1]
    // do something, Gromit!
    match = myregexp.exec(subject);
}