Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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,有没有一种方法可以让RegExp在给定regex/aa/g(或类似值)的情况下匹配字符串“aaa”的两倍?第一场比赛是前两个a,第二场比赛是最后两个a 大概是这样的: 比赛一:aa'a 比赛2:a'aa' 您可以捕获两个a的内部前瞻,然后返回并匹配一个a角色 let模式=/(?=(a{2}))a/g; 让resultMatches=[]; 让我们比赛; 设stringToCheck='aaa'; while((match=pattern.exec(stringToCheck))!==null

有没有一种方法可以让RegExp在给定regex/aa/g(或类似值)的情况下匹配字符串“aaa”的两倍?第一场比赛是前两个a,第二场比赛是最后两个a

大概是这样的:

  • 比赛一:aa'a
  • 比赛2:a'aa'

您可以捕获两个a的内部前瞻,然后返回并匹配一个a角色

let模式=/(?=(a{2}))a/g;
让resultMatches=[];
让我们比赛;
设stringToCheck='aaa';
while((match=pattern.exec(stringToCheck))!==null)
结果匹配推送(匹配[1]);

console.log(结果匹配)您可以捕获两个a的内部前瞻,然后返回并匹配一个a字符

let模式=/(?=(a{2}))a/g;
让resultMatches=[];
让我们比赛;
设stringToCheck='aaa';
while((match=pattern.exec(stringToCheck))!==null)
结果匹配推送(匹配[1]);

console.log(结果匹配)您可以将
reg
lastIndex
更改为
index+1

let str=“aaa”,
reg=/aa/g,
下一步=注册执行官(str),
res=[];
while(下一个){
res.push(下一步[0]);
reg.lastIndex=next.index+1;
下一步=注册执行(str);
}

控制台日志(res)您可以将
reg
lastIndex
更改为
index+1

let str=“aaa”,
reg=/aa/g,
下一步=注册执行官(str),
res=[];
while(下一个){
res.push(下一步[0]);
reg.lastIndex=next.index+1;
下一步=注册执行(str);
}
控制台日志(res)