Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda