0

How can I get ['999'] out of this string? '451999277'? I only want repetitions of the same character.

This is what I've tried:

'451999277'.match(/(\d{3})/g) // === ["451", "999", "277"]
'451999277'.match(/(\d){3}/g) // === ["451", "999", "277"]
'451999277'.match(/([0-9]){3}/g) // === ["451", "999", "277"]
'451999277'.match(/(\d)\1{3}/g) // === null

.......

[EDIT]

solution:

'451999277'.match(/(\d)\1{2}/g) // ===  ['999']
Matthew Masurka
  • 325
  • 3
  • 14

1 Answers1

1

You're almost there with your last example. If you just want groups of 3 use {2} instead of {3}:

console.log('4519992277'.match(/(\d)\1{2}/g))

console.log('455519992277'.match(/(\d)\1{2}/g))
Mark
  • 90,562
  • 7
  • 108
  • 148