检查字符串是否包含特殊字符且数字和字母中至少有 2 个字符
大家好,我试图检查密码是否包含特殊搬运工和至少2个字符(数字或字母) 如此这样:
("&#€$&&÷") ====> false
("&#&'&*#5") ====> false
(">~<<`<•5t") ====> true
("{\><>\tt") =====> true
("65%#^$*@") ====> true
("7373673") ====> false
("7267373~") ====> true
我尝试过这种正则表达式,但似乎不起作用:
/^((?=.*\d{2})|(?=.*?[a-zA-Z]{2}))/
Hi guys I trying to check if password contains special caracters and at least 2 characters (digit or letter)
So like this:
("€amp;&÷") ====> false
("&'&*#5") ====> false
(">~<<`<•5t") ====> true
("{\><>\tt") =====> true
("65%#^$*@") ====> true
("7373673") ====> false
("7267373~") ====> true
I've tried this regular expression but It seems not working:
/^((?=.*\d{2})|(?=.*?[a-zA-Z]{2}))/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在同一个前瞻中断言某个字符或数字出现两次,然后至少匹配一个“特殊”字符。
使用不区分大小写的模式:
该模式匹配:
^
字符串开头(?:[^az\d\n]*[az\d]){2}
断言字符 az 或数字出现 2 次。[^az\d\n]*
部分使用[^
否定字符类,以防止不必要的回溯[az\d]*
匹配可选字符 az 或数字[~!@#$%^&*()_+<>•`{}\]
匹配特殊字符[~!@#$%^&()_+<>•`{}\az\d]
匹配可选允许的字符$< /code> 字符串结尾
正则表达式演示
You can assert the 2 occurrences of a character or digit in the same lookahead, and then match at least a single "special" character.
Using a case insensitive pattern:
The pattern matches:
^
Start of string(?:[^a-z\d\n]*[a-z\d]){2}
Assert 2 occurrences of either a char a-z or a digit. The[^a-z\d\n]*
part negates the character class using[^
to prevent unnecessary backtracking[a-z\d]*
Match optional chars a-z or a digit[~!@#$%^&*()_+<>•`{}\]
Match a special character[~!@#$%^&()_+<>•`{}\a-z\d]
Match optional allowed chars$
End of stringRegex demo