How to count the number of same occurring characters in a string

Using replace

(.)\1+ demo

var str="AABAAABBC"

var res = str.replace(/(.)\1+/g, (match, p1) => match.length + p1)

console.log(res)

Leave a Comment