问题描述:

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.

网友答案:

Try using a positive lookahead to ascertain that there's at least one digit, and use {3,} to indicate that it should match at least 3 characters:

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

You can use lookahead to assure your expression contains a digit, and then match the minimum-three-chars:

/^(?=.*?\d)[a-zA-Z0-9]{3,}$/
相关阅读:
Top