javascript – Multiple options in RegEx

Question:

I have to validate an input , which contains a Contract code.

The formats can be:

  • AAAXXXX or AAAXXXX/XX-XX or AAAXXXX/VXXXX
  • AXXXXXX or AXXXXXX/VXXXX

(Where A an alphabetic character, X a digit and the rest ( / - V ) are literal)


The regex that I have raised is this:

/^((\w{3}\d{4}(\/\d{2}-\d{2})?)|(((\w\d{6})|(\w{3}\d{4}))(\/v\d{4})?))$/i

I have some test cases online. It works fine for all cases except the last one, which takes it for valid when it shouldn't:

C123456/30-02

How would the resulting so that it fulfills all cases?

Answer:

It is an error to assume that \w matches only letters, when az , AZ , 0-9 and _ (equivalent to [A-Za-z0-9_] ), so the pairing is done with the first option found ( AAAXXXX/XX-XX since A includes X ).

This could be the correct pattern:

^(([A-Za-z]{3}\d{4}(\/\d{2}-\d{2})?)|((([A-Za-z]\d{6})|([A-Za-z]{3}\d{4}))(\/v\d{4})?))$ (View online)

If you are not going to make use of the data obtained in each match group, it is better to use non-capturing parentheses (?: X) :

^(?:[az]{3}\d{4}(?:\/(?:\d{2}-\d{2}|V\d{4}))?|[az]\d{6}(?:\/v\d{4})?)$ (View online)

To facilitate debugging and maintenance of the regular expression I keep the groups as you define them in your rules. It will not impact performance as the code will only be executed at the request of the user (in the form submit event).

Scroll to Top