Is there any way in js to use RegExp and str.match()
to count combinations of substrings in a string? I'm trying to achieve the following:
given a string:
'999999000'
I need to find all the combination of 99
that exist in the string, the expected result for the above string is 5 as you can combine the indexes in the following pairs to create a 99
:
index 0 and 1
index 1 and 2
index 2 and 3
index 3 and 4
index 4 and 5
now I've tried using the match method the following way:
let re = new RegExp("99","gi");
let matches = "999999000".match(re).length;
console.log(matches);
but the result it throws it's 3.
note that the above snippet would work for the following case:
let re = new RegExp("99","gi");
let matches = "00990099099099".match(re).length;
console.log(matches);
I know this problem can be solved by iterating on the string to find all the'99' combinations, but I want to solve it using regex and str.match