There is a mask for the number A of BBBBB A. For this mask, wrote a regular expression

/([0-9])\1{0}([0-9])\2{4}([0-9])\3{0}/ 

But the result is that, for example, the number 2555553 also fits this mask (as well as, for example, 5555552). As I understand it, I incorrectly understand the principle of the group.
Is it possible to describe this mask with one regular expression or will it be necessary to add a crutch (the first and last digits are equal and there are no repetitions more than 5 times)?

  • /([0-9])\1{0}([0-9])\2{4}\1{1}/ - Yaant
  • @Yaant, all the same also fall under this regular season (A! = B) - Ajmda
  • Nowhere in the text of the question was it mentioned that A should not be equal to B. Therefore, the existence of such a requirement did not even occur to me. :) - Yaant

1 answer 1

 var n = prompt('Введите симметричное число', 1888881); if (n.match(/^(\d)(?!\1)(\d)\2{0,4}\1$/)) { console.log('подходит'); } else { console.log('неа'); } 

^ - beginning of line
(\d) - the first digit
(?!\1) - the next character does not match the first digit
(\d) - the second digit
\2{0,4} - after it from 0 to 4 of the same numbers
\1 - and again the first digit
$ - end of line

  • Thank. And if I needed it so that the first number was different from the last? - Ajmda
  • one
    \1$ replace with (?!\1)\d$ - Visman