javascript 正则判断车牌号 in javascript

To validate a Chinese car plate number using regular expression in JavaScript, you can use the following code:

index.tsx
function isCarPlateNumberValid(plateNumber) {
  // Create a regular expression pattern for the Chinese car plate number
  const pattern = /^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$/;
  
  // Return true if the plate number matches the pattern, false otherwise
  return pattern.test(plateNumber);
}

// Example usage
console.log(isCarPlateNumberValid('粤A12345'));  // true
console.log(isCarPlateNumberValid('粤A1234'));   // false
console.log(isCarPlateNumberValid('粤ABC123'));  // false
485 chars
13 lines

Explanation:

  • The regular expression pattern ^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$ matches a string that starts with exactly one Chinese character ([\u4e00-\u9fa5]{1}), followed by exactly one uppercase English letter ([A-Z]{1}), and then followed by five uppercase English letters or digits ([A-Z_0-9]{5}).

  • The test() method of the regular expression object checks whether a string matches the pattern, and returns a boolean value.

  • The isCarPlateNumberValid() function takes a string argument plateNumber and returns true if it matches the regular expression pattern, false otherwise.

  • The example usage shows how to use the isCarPlateNumberValid() function with some sample inputs.

gistlibby LogSnag