-6

I am having a string like the follows: "AABAAABBC"

I need to implement javascript logic to print the following output.

2AB3A2BC

yousuf
  • 31
  • 6
  • Possible duplicate of [Count the number of occurrences of a character in a string in Javascript](https://stackoverflow.com/questions/881085/count-the-number-of-occurrences-of-a-character-in-a-string-in-javascript) – 123survesh Sep 16 '19 at 12:09
  • 1
    https://stackoverflow.com/questions/881085/count-the-number-of-occurrences-of-a-character-in-a-string-in-javascript – 123survesh Sep 16 '19 at 12:10
  • The suggested duplicate counts characters anywhere in the string, according to its sample output OP wants to count consecutive characters – Aaron Sep 16 '19 at 12:12

1 Answers1

3

Using replace

(.)\1+ demo

var str = 'AABAAABBC'

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

console.log(res)
User863
  • 19,346
  • 2
  • 17
  • 41