简体   繁体   中英

Regular expression for alphanumeric with atleast one digit

I am trying to match an alphanumeric string with at least one digit. The second condition is that it's minimum length should be 3.

For example, the following strings should match

111
12345
ABCD1
123A
11AA11

And the following should not match

ABCD
AB
12
1A

I have got myself to a point where I can get the first condition right. That is, having minimum one digit:

([a-zA-z0-9]*[0-9]+[a-zA-z0-9]*)

But I don't have any idea to specify a minimum length. If I try using {3}, it will require minimum of 3 numbers.

尝试使用正向前瞻来确定至少有一位数字,并使用{3,}表示它应至少匹配3个字符:

/^(?=.*\d)[a-z\d]{3,}$/i

您可以使用lookahead来确保表达式中包含一个数字,然后匹配最少三个字符:

/^(?=.*?\d)[a-zA-Z0-9]{3,}$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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