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

108
Visualizações
Regex limit the number of letters total in the entire string

I have this expression which is almost what I need:

/(?=^.{5,12}$)(^[a-zA-Z\u0590-\u05fe]{0,4}[0-9]{3,8}[a-zA-Z\u0590-\u05fe]{0,4}$)/

except, I need to only allow 4 letters total (4 in the beginning and 0 in the end,3 and 1, 2 and 2, 0 and 4, etc...)

allowed inputs: 11abcd11 1abcdefg123 abcd1234

unallowed inputs: 1abcd11 abcd123 1abcd12

is there a way to achieve this? thanks!

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

To make sure there are exactly four letters in the string, you can use

^(?=.{5,12}$)(?=(?:[^a-zA-Z\u0590-\u05fe]*[a-zA-Z\u0590-\u05fe]){4}[^a-zA-Z\u0590-\u05fe]*$)[a-zA-Z\u0590-\u05fe]{0,4}[0-9]{3,8}[a-zA-Z\u0590-\u05fe]{0,4}$

See the regex demo

The (?=(?:[^a-zA-Z\u0590-\u05fe]*[a-zA-Z\u0590-\u05fe]){4}[^a-zA-Z\u0590-\u05fe]*$) positive lookahead makes sure there are four sequences of any zero or more "non-letters" followed with a "letter" and then there are zero or more "non-letters" till the end of string.

If you can target ECMAScript 2018+ compliant engines you can use a Unicode-aware version:

/^(?=.{5,12}$)(?=(?:\P{L}*\p{L}){4}\P{L}*$)\p{L}{0,4}[0-9]{3,8}\p{L}{0,4}$/u

See this regex demo, where \p{L} matches letters. You may also replace it with \p{Alphabetic} which also matches letters that are Roman numbers.

about 4 years ago · Santiago Gelvez 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