This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

During the project development, I met the demand of regularization verification of license plate number and began to ponder over it. I thought I could write out the data quickly, but I struggled for a long time with my weak regular foundation. Let me regain the confidence to learn regular. Refer to the Internet scattered rules and nuggets on the system of regular expressions complete tutorial (slightly longer), can write a license plate requirements. Here from the license plate number of the regular verification. Rules:

• Car sub oil vehicles, pure electric vehicles, hybrid oil – electric vehicles. The length of the license plate of the oil vehicle is 7, and the length of the parking space of the pure electric vehicle and the hybrid car is 8
``````const len = plateNo.length;
// Digit check
if(len ! = =7&& len ! = =8) return false;
Copy the code``````
• The first plate number represents the province, and the abbreviation chosen in the province is: Beijing, Tianjin, Shanghai, Chongqing, Hebei, Yun, Liao, Heihe, Xiang, Anhui, Lu, Xinsu, Zhejiang, Jiangxi, Hubei, GUI, Gan, Jin, Mongolia, Shaanxi, Ji, Min, GUI, Guangdong, Qinghai-Tibet, Sichuan, Ningqiong, all the first must be the first in the 31 provinces, and the regular is:
``/^[Beijing, Tianjin, Shanghai, Chongqing, Hebei, Yunliao, Heixiang, Anhui, Lu, Xinsu, Zhejiang, Hubei, GUI, Gan, Shanxi, Mengshan, Ji, Min, GUI, Guangdong, Qinghai, Sichuan, Ningqiong]{1} /Copy the code``
• The second digit of the license plate number represents the first-class code of the city where the car is located. It consists of 26 English letters, excluding I and O. To distinguish it from the numbers. That is, a-h, J-n, p-z, regular is:
``/[A-HJ-NP-Z]{1} /Copy the code``
• The last 5 digits are composed of 0 to 9 digits, A-H, J-N, p-Z, and the number of letters is not more than 2. The rule is:
``````/([A-HJ-NP-Z0-9] {5}) /// Contains a maximum of two characters
const arr = plateNo.slice(2).match(/[A-HJ-NP-Z]/g);
if (arr.length > 2) return false;
Copy the code``````
• Pure electric car, hybrid car, last 6, various situations
• The pure digital
``/ [0-9] {6} /Copy the code``
• 1 random letter + 5 random numbers
``/[A-HJ-NP-Z]{1} [0-9] {5} /Copy the code``
• 5 random numbers + 1 random letter
``/ [0-9] {5}[A-HJ-NP-Z]{1} /Copy the code``
• 2 random letters + 4 random numbers
``/[A-HJ-NP-Z]{2} [0-9] {4}) /Copy the code``

To summarize, the full version of the function:

``````* Check the license plate * @param {string} plateNo license plate * @returns {Boolean} Whether the license plate rules are mettrue: in accordance withfalse: Does not comply with */function checkPlateNumberFormat(plateNo: string) {
if(! plateNo)return false;
const len = plateNo.length;
// Digit check
if(len ! = =7&& len ! = =8) return false;
// Check the license plate rule
const PLATE_NO_PATTERN = / ^ [beijing-tianjin Shanghai yu ji yu cloud liao black xiang anhui new GuiGan of hubei province can Sue your yue jin meng shan ji min qinghai-tibet plain NingQiong] {1} [A - HJ - NP - Z] {1} (((A - HJ - NP - Z0-9] {5}) | ([0-9] {6} | [A - HJ - NP - Z] {1} [0-9] {5} | [0-9] {5} [HJ - A NP-Z]{1}|[A-HJ-NP-Z]{2}[0-9]{4}))\$/;
if (PLATE_NO_PATTERN.test(plateNo)) {
// New energy vehicles
if (len === 8) return true;
// The last five digits contain a maximum of two characters
const arr = plateNo.match(/[A-HJ-NP-Z]/g);
if (arr.length <= 3) return true;
}
return false;
}
Copy the code``````