For example, I have a following string that consists of 0 and 1: 1010101101
or anything close to it. How am I able to count number of all repeated 3-number sequences of elements in it?
Simple split and regex doesn’t seem to work because when I search for number of repeats of 101
it doesn’t get the right amount.
So I guess I need some method to manually iterate through the string with for loop and if’s to check every possible combination. Is there a way to make it faster in case there are much more numbers?
I haven’t found any question like that at stackoverflow so I guess it’s not a duplicate.
Thank you in advance!
Asked
Active
Viewed 127 times
0
-
Which exactly sequences? Any? – Styx Apr 17 '19 at 12:38
-
[RegExp match repeated characters](https://stackoverflow.com/questions/6306098/regexp-match-repeated-characters) maybe help you – Mohammad Apr 17 '19 at 12:39
1 Answers
1
You can use a positive lookahead regex pattern:
var temp = "00111010101101";
var count = (temp.match(/(?=101)/g) || []).length;
console.log(count);

blhsing
- 91,368
- 6
- 71
- 106
-
-
1@Slim, no, it doesn’t. In my case it ignores one “101” because it goes right after the previous one. – LionKing Apr 17 '19 at 13:02
-