Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
Match 10 digit alternating numbers

I want a regex which matches 10 digit alternating numbers like 1212121212 3434343434 so on. I know this is not right way to ask here. But I tried a lot from my side I was not able to figure out so reaching out for help.

It cannot be 1313131313. I should be the next number.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If the string is always 10 characters long and we always have single-digit numbers then simply take the first two characters, repeat 5 times and compare:

const check = str => {
  const a = +str[0];
  const b = +str[1];
  return a+1 === b && `${a}${b}`.repeat(5) === str;
};

check('1212121212');
//=> true

check('1212121215');
//=> false

check('1313131313');
//=> false
about 4 years ago · Juan Pablo Isaza Relatório

0

You might also use Array.from and specify a length and an offset to get the alternation for the next number:

const alternating = (len, offset) =>
  Array.from({ length: len }, (_, index) => (index % 2) + offset).join('');

console.log(alternating(10, 1) === "1212121212");
console.log(alternating(10, 3) === "3434343434");
console.log(alternating(10, 2) === "1313131313");

about 4 years ago · Juan Pablo Isaza Relatório

0

For pure regex you would just have to work out the different combinations because regex doesn't support the concept of numbers nor their sequence; everything is a string of chars.

^(01|12|23|34|45|56|67|78|89)\1{4}$

If 9090909090 is valid then:

^(01|12|23|34|45|56|67|78|89|90)\1{4}$
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda