REGEX control syntax

Introduction:
On a custom form, I have a field for which the syntax has to be controled
I understand Regex is THE solution, but I am not familiar at all with this nice feature.

The input should be
either : PN followed by a number between 1 and 99
or : J followed by a number between 1 and 99, then a dash , and another a number between 1 and 99

exemples
PN1, PN13, PN49 are correct
J1-4, J1-56, J35-42 are correct
AB5, A456, J56, JN3, J455, J8745, PN345 are not correct

I tested with /^PN[0-9]{1,2}|^J[0-9]{1,2},[-],[0-9]{1,2}/
but it does not work as I would like

  • PN345 is accepted, although {1,2} should (with my poor understanding) limit to 1 or 2 figures
  • J35-45 is not accepted, but should be

Can somebody help me in setting the regex properly

Should the result look like …

If so… your REGEX…
/^(PN[1-9][0-9]?|J[1-9][0-9]?-[1-9][0-9]?)$/gm

1 Like