是否可以使用以下规则来验证英国的驾驶执照?



到目前为止,我有以下内容,但无法正常工作:

var regex = /^[A-Z0-9]{5}\d[0156]\d([0][1-9]|[12]\d|3[01])\d[A-Z0-9]{3}[A-Z]{2}$/

应该通过以下内容:
  [
    'FARME100165AB5EW',
    'MAR99740614BC2TL',
    'MARTI740614A92TL',
    'MARTI740614992TL',
    '99999740614992TL'
  ].forEach(drivingLicenceNumber => regex.test(drivingLicenceNumber)

只有数组的第一个元素通过。

并且以下应该失败
  [
    '1FARM382940AZ9AZ',
    'F2ARM382940AZ9AZ',
    'FA3RM382940AZ9AZ',
    'FAR4M382940AZ9AZ',
    'FARM5382940AZ9AZ'
  ].forEach(drivingLicenceNumber => !regx.text(drivingLicenceNumber)

最佳答案

这应该工作:

^[A-Z9]{5}\d{6}[A-Z9]{2}\d[A-Z]{2}$

说明:
  • [A-Z9]{5}数字1-5:字母A-Z或9的
  • \d{6}数字6-11:数字0-9
  • [A-Z9]{2}数字12-13:字母A-Z或9的
  • \d数字14:数字0-9
  • [A-Z]{2}数字15–16:两个字母A-Z


  • var regex = /^^[A-Z9]{5}\d{6}[A-Z9]{2}\d[A-Z]{2}$$/
    
    console.log('all pass');
      [
        'FARME100165AB5EW',
        'MAR99740614BC2TL',
        'MARTI740614A92TL',
        'MARTI740614992TL',
        '99999740614992TL'
      ].forEach(drivingLicenceNumber => {
        console.log(drivingLicenceNumber, regex.test(drivingLicenceNumber));
      });
    console.log('all fail');
      [
        '1FARM382940AZ9AZ',
        'F2ARM382940AZ9AZ',
        'FA3RM382940AZ9AZ',
        'FAR4M382940AZ9AZ',
        'FARM5382940AZ9AZ'
      ].forEach(drivingLicenceNumber => {
        console.log(drivingLicenceNumber, regex.test(drivingLicenceNumber));
      }); 

    07-26 01:21