怎么样:
/^(?=2.*\d)[a-zA-Z0-9]{8}$/
如果该数字2
是所需的 2 个数字之一。
/^(?=2.*\d.*\d)[a-zA-Z0-9]{8}$/
如果该数字2
不计入 2 个所需数字之一。
解释:
The regular expression:
(?-imsx:^(?=2.*\d)[a-zA-Z0-9]{8}$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
2 '2'
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[a-zA-Z0-9]{8} any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (8 times)
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------