0

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!

blhsing
  • 91,368
  • 6
  • 71
  • 106
LionKing
  • 131
  • 3
  • 12

1 Answers1

1

You can use a positive lookahead regex pattern:

var temp = "00111010101101";
var count = (temp.match(/(?=101)/g) || []).length;
console.log(count);

Demo: https://regex101.com/r/JAvzvv/1

blhsing
  • 91,368
  • 6
  • 71
  • 106