javascript版本

  1. /**
  2. * 车牌号验证(后半部分不能出现I和O,避免与0和1混淆)
  3. * @param vehicleNumber
  4. * @returns {*}
  5. */
  6. export function isVehicleNumber(vehicleNumber) {
  7. var xxreg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DABCEFGHJK]$)|([DABCEFGHJK][A-HJ-NP-Z0-9][0-9]{4}$))/; // 2021年新能源车牌不止有DF
  8. var creg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
  9. if(vehicleNumber.length == 7){
  10. return creg.test(vehicleNumber);
  11. } else if(vehicleNumber.length == 8){
  12. return xxreg.test(vehicleNumber);
  13. } else{
  14. return false;
  15. }
  16. }

java版本

  1. final static Pattern p = Pattern.compile("^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$");
  2. public static void main(String[] args) {
  3. String carNo = "京A123435";
  4. if (carNo.length() == 7 || carNo.length() == 8) {
  5. Matcher m = p.matcher(carNo);
  6. if (!m.matches()) {
  7. System.out.println(false);
  8. return;
  9. }
  10. System.out.println(true);
  11. } else {
  12. System.out.println(false);
  13. }
  14. }