I searched over the title and found some solutions, but none of them worked for me. I want something like following:
checkRepeat('ccc','cc'); // should give output 2
checkRepeat('cccaacddcccc','cc'); // should give output 5
and so on. Please help me with this.
What I've tried:
function checkRepeat(string, search) {
if (!search.length) return 0;
var pattern = new RegExp('(' + search + ')', 'ig'),
match = string.match(pattern),
parts = string.split(pattern).slice().filter(function (i) {
return i.length;
});
console.log(match.length);
console.log(parts.length - 1);
}
checkRepeat('cccaacddcccc', '')should return 13, since it "occurs" in between each letter. By the way, it's highly unlikely regexp is going to work for this, since you're looking for matches which are overlapping. Your solution is not working how? @Begueradj Downvotes are not unreasonable for "gimme-some-code" questions, not matter what the rep is.