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']
5
  • 3
    Possible duplicate of Regex to find repeating numbers Commented Sep 12, 2018 at 17:19
  • 4
    (\d) matches one digit, \1{3} matches three digits, so (\d)\1{3} would require four digits to match. Commented Sep 12, 2018 at 17:26
  • Gotcha. Thanks! This is the winner: '451999277'.match(/(\d)\1{2}/g) Commented Sep 12, 2018 at 17:40
  • 1
    As a nit-pick, I object to your use of === in the comment. I'd like to point out that even ['999'] === ['999'] is false. We get what you meant though. Commented Sep 12, 2018 at 17:48
  • @Wyck ah yeah good point! Commented Sep 13, 2018 at 18:27

1 Answer 1

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))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.