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

119
Visualizações
Regex for an alphanumeric string of specific length with at least one letter and at least one digit

I want to match alphanumeric string of specific length with at least one letter and at least one digit.

For example, adfyg432 should contain alphabetic and digit and the length should start from 8.

I used this expression but it won't work:

^([A-Za-z]{1,}\d{1,}){8,}$
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your current pattern repeats a group 8 or more times. That group by itself matches 1 or more chars a-z followed by 1 or more digits.

That means that the minimum string length to match is 16 chars in pairs of at least 2. So for example a string like a1aa1a1a1a1a1a1a1 would match.


You could write the pattern using 2 lookahead assertions to assert a length of at least 8 and assert at least a char a-z.

Then match at least a digit. Using a case insensitive match:

^(?=[a-z\d]{8,}$)(?=\d*[a-z])[a-z]*\d[a-z\d]*$

In parts, the pattern matches:

  • ^ Start of string
  • (?=[a-z\d]{8,}$) Positive lookahead, assert 8 or more chars a-z or digits till end of string
  • (?=\d*[a-z]) Positive lookahead to assert at least a char a-z
  • [a-z]* Match optional chars a-z
  • \d Match at least a single digit
  • [a-z\d]* Match optional chars a-z or digits
  • $ End of string

Regex demo

const regex = /^(?=[a-z\d]{8,}$)(?=\d*[a-z])[a-z]*\d[a-z\d]*$/i;
[
  "AdfhGg432",
  "Abc1aaa"
].forEach(s =>
  console.log(`Match "${s}": ${regex.test(s)}`)
)

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