Identify repeated numeric characters in sequence

3

With expression [\d]{9} I can identify numeric characters if they are repeated nine times in sequence, however I only want identify if they are the same characters, for example:

111111111 // false
222222222 // false
333333333 // false
077546997 // true
123566566 // true

How should this grouping be handled?

    
asked by anonymous 05.10.2016 / 15:18

3 answers

3

You can use this way: (?!(\d){8})\d{9}

Basically two parts:

  • what should not be caught: (?!(\d){8})
  • the expected format of string \d{9}

The negative look ahead part of the (?! .... ) tag looks for strings that should not be accepted. Well, make a catch group and then use that catch as a reference with as I explained in this other response . Basically it takes the first number and says that it must have 8x the same number (within this deny field).

Example: link

    
05.10.2016 / 16:07
2

You can use the (\d){8} expression.

Where:

  • (\d) : Capture group, only 0-9 digits.
  • {8} : Refers to the first catch group. It will correspond to the same pattern matched by the catch group, eight times. See more details here .

console.log(/(\d){8}/.test('111111111'));
console.log(/(\d){8}/.test('222222222'));
console.log(/(\d){8}/.test('333333333'));
console.log(/(\d){8}/.test('444444444'));
console.log(/(\d){8}/.test('077546997'));

Editing

The expression used in the response will match the numbers repeated in sequence, to do the opposite, see @Sergio and @Allan !

    
05.10.2016 / 16:07
1

One option is to use the regular expression below and deny its output:

0{9}|1{9}|2{9}|3{9}|4{9}|5{9}|6{9}|7{9}|8{9}|9{9}

Result:

  

111111111 // home

     

222222222 // home

     

333333333 // home

     

077546997 // not home

     

123566566 // not home

    
05.10.2016 / 15:32