开发者

Regex to accept at least one alpha char and numeric char

开发者 https://www.devze.com 2023-04-07 02:03 出处:网络
I was trying out a regex to \"partial match\" (any place in the string): abcd1234 1a2b I searched for a regex and found this:

I was trying out a regex to "partial match" (any place in the string):

abcd1234 1a2b

I searched for a regex and found this:

/^(?=.*\d)(?=.*[a-zA-开发者_Python百科Z])$/

But it accepts only alphanumeric; abcd123!@#$ is not matched.

How can this be fixed?


How about this?

/^.*[a-zA-Z].*\d.*|.*\d.*[a-zA-Z].*$/

This should match either:

  • an alphabetic character somewhere, followed by a numeric character somewhere, with any number of other types of characters on either side or between them; or
  • the other way around (numeric followed by alphabetic)


If I understood you correctly, this is what you want:

/^.*[a-zA-z].*\d.*/

/*
'2344' => false
'abcd' => false
'a1cd' => true
'abc3' => true
'ab@3' => true
'a_*3' => true
'2_!b' => false
*/


The following will allow the alpha and the numeric to appear in either order:

/^.*((\d.*[a-zA-Z])|([a-zA-Z].*\d)).*$/

0

精彩评论

暂无评论...
验证码 换一张
取 消