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计算使用js在字符串中由相同字符组成的substr_Javascript_Regex_String - Fatal编程技术网

Javascript 如何使用RegExp计算使用js在字符串中由相同字符组成的substr

Javascript 如何使用RegExp计算使用js在字符串中由相同字符组成的substr,javascript,regex,string,Javascript,Regex,String,js中是否有任何方法可以使用RegExp和str.match()来计算字符串中的子字符串组合?我正在努力实现以下目标: 给定一个字符串: '999999000' 我需要找到字符串中存在的99的所有组合,上面字符串的预期结果是5,因为您可以组合以下成对的索引来创建99: index 0 and 1 index 1 and 2 index 2 and 3 index 3 and 4 index 4 and 5 现在,我尝试使用match方法,方法如下: let re=new RegExp(“9

js中是否有任何方法可以使用RegExp和
str.match()
来计算字符串中的子字符串组合?我正在努力实现以下目标:

给定一个字符串:

'999999000'
我需要找到字符串中存在的
99
的所有组合,上面字符串的预期结果是5,因为您可以组合以下成对的索引来创建
99

index 0 and 1
index 1 and 2
index 2 and 3
index 3 and 4
index 4 and 5
现在,我尝试使用match方法,方法如下:

let re=new RegExp(“99”,“gi”);
让matches=“9999000”。匹配(re)。长度;
console.log(匹配项)您可以使用一个正数来断言2次9:

const字符串=[
"999999000",
"00990099099099"
];
设re=newregexp((?=(9{2})),“gi”);
strings.forEach((s)=>{
console.log(s+“==>”+s.match(re.length);
});您可以使用一个正数来断言2次9:

const字符串=[
"999999000",
"00990099099099"
];
设re=newregexp((?=(9{2})),“gi”);
strings.forEach((s)=>{
console.log(s+“==>”+s.match(re.length);

});现在解决了该特定情况下的问题,我如何在regExp对象中使用变量而不是'99'我将子字符串存储在变量X中?在本例中,您可以使用regExp构造函数并传递变量
new regExp((?=(“+variable+”{2})),“gi”)。我认为可以帮助您转义作为变量传递的内容。谢谢,我使用了您的答案来解决我的问题,您的答案仅在子字符串长度为2时对我有效,但在其他情况下会失败,我最终会执行以下操作:
let P=“09999999999999090999”;let test=“(?=(“+P+”{1}))”let re=new RegExp(test,“gi”);让matches=arrayG.match(re).length现在解决了该特定情况下的问题,我如何在regExp对象中使用变量而不是'99'我将子字符串存储在变量X中?在本例中,您可以使用regExp构造函数并传递变量
new regExp((?=(“+variable+”{2})),“gi”)。我认为可以帮助您转义作为变量传递的内容。谢谢,我使用了您的答案来解决我的问题,您的答案仅在子字符串长度为2时对我有效,但在其他情况下会失败,我最终会执行以下操作:
let P=“09999999999999090999”;let test=“(?=(“+P+”{1}))”let re=new RegExp(test,“gi”);让matches=arrayG.match(re).length