简体   繁体   English

Javascript Regex验证字符串长度和后3个数字字符

[英]Javascript Regex to validate string length and last 3 characters as digits

How should I write regex to validate below conditions: 我应该如何编写正则表达式来验证以下条件:

  • Total length of string should be between 4 to 6 字符串的总长度应在4到6之间
  • Last 3 characters should be digits only. 后3个字符应仅是数字。
  • String is only alphanumeric 字符串仅是字母数字

eg: Valid strings: 1234, EC123, 1YC898, 001234 例如:有效字串:1234,EC123、1YC898、001234

So far, I have tried below regex, but seems like I am missing something? 到目前为止,我已经尝试过使用正则表达式,但是好像我缺少了什么?

(^[a-zA-Z0-9]{4,6})?\d{3}$

You can use: 您可以使用:

^[a-zA-Z0-9]{1,3}\d{3}$
  • ^[a-zA-Z0-9]{1,3} will match 1 to 3 alpha-numerals at the start ^[a-zA-Z0-9]{1,3}将在开始时匹配1到3个字母数字
  • \\d{3}$ will match 3 digits at the end of your input \\d{3}$将在您输入的末尾匹配3位数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM