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?
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.