2

I have a working version of PCRE regular expression that checks is a given string matches French licence plates. It goes like this (live demo):

/^(?(?!ss|ww)[a-hj-np-tv-z]{2})(?:\s|-)?[0-9]{3}(?:\s|-)?(?(?!ss)[a-hj-np-tv-z]{2})$/i

Now I would like to transform it to JavaScript regex syntax. I understand that conditional regex syntax used here is not supported by JavaScript regex engine, so I should rewrite it.

I've started with a first group which:

  1. Has to have two letters
  2. They can't be SS nor WW
  3. They can't use I, O, U

This is what I've come up with (live demo):

/^(?!(?=(?:ss|ww))|(?![a-hj-np-tv-z]{2}))/i

And it works. Now I would like to add second part:

  1. The first group can be (but doesn't have to be) followed by a white space character or a hyphen.
  2. After that there is a group of exactly 3 digits.

So I've tried to add (?:\s|-)?[0-9]{3} and it fails. I guess it may have something to do with the fact that the very first group is not consumed, but I don't understand why. Optionally if anyone would also be able to help me with info on how to create a capturing group of the group of first two letters (I don't need it now, but hat would help me understand the process).

george007
  • 609
  • 1
  • 7
  • 18
  • Don't forget that cars bought before 2009 may have always the old plate format (the one used since the 50's). – Casimir et Hippolyte Jun 16 '16 at 10:36
  • Yes, I will have them covered with this regular expression which works for both PCRE and JavaScript engines: `/^[0-9]{1,4}(?:\s|-)?[a-hj-np-tv-z]{2,3}(?:\s|-)?(?:97[1-6]|0[1-9]|[1-8][0-9]|9[1-5]|2[ab])$/i`. Thanks for pointing it out. – george007 Jun 16 '16 at 13:20

2 Answers2

3

I guess you can simply use the following:

/^(?!ss|ww)[a-hj-np-tv-z]{2}[ -]?[0-9]{3}[ -]?(?!ss)[a-hj-np-tv-z]{2}$/i

It seems to be working well for me.

VisioN
  • 143,310
  • 32
  • 282
  • 281
1

The begining can be

/^(?!ss|ww|.[iou]|[iou].)[a-z]{2}[-\s]?\d{3}/i

And the full expression is

/^(?!ss|ww|.[iou]|[iou].)[a-z]{2}[-\s]?\d{3}[-\s]?(?!ss|ww|.[iou]|[iou].)[a-z]{2}$/i
Qwertiy
  • 19,681
  • 15
  • 61
  • 128