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

162
Visualizações
How do I allow one of the two inputs from a user to match the regex pattern?

So I have two different regular expressions for zipcodes from two different countries. One being The Netherlands and the other one being Belgium.

The Netherlands:

.match(/^[1-9][0-9]{3} ?(?!sa|sd|ss)[A-Za-z]{2}$/g)

Belgium:

.match(/^[1-9]{1}\d{3}$/g)

The user can input either a dutch zipcode or a belgium zipcode. So for example the user can input: 1111 AA (Netherlands) or 1111 (Belgium). However I want regex to allow one of the two inputs, but I just can't get it to work

This is what I have tried so far:

.match(/^(?:[1-9]{1}\d{3}) | ([1-9][0-9]{3} ?(?!sa|sd|ss)[A-Za-z]{2}$)/g)

But it just messes up the whole regex and doesn't allow any of the 2 zipcodes. Am I misusing the OR operator?

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

0

It seems you have mistakenly put a leading/trailing space character around the alternation. However, it seems you can remove the alternation alltogether and use an optional non-capture group:

^[1-9]\d{3}(?: ?(?!s[ads])[A-Z]{2})?$

See an online demo


  • ^ - Start line anchor;
  • [1-9]\d{3} - A digit ranging 1-9 followed by another three digits;
  • (?: - Open non-capture group;
    • ?(?!s[ads]) - An optional space followed by an negative lookahead to assert position isn't followed by any combination of an 's' with 'ads';
    • [A-Z]{2} - Two alpa-chars;
    • )? - Close non-capture group and make it optional;
  • $ - End-line anchor.

Note that I matched this case-insensitive.

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