简体   繁体   English

Javascript-正则表达式两个大写,小写和数字

[英]Javascript - Regular Expression For two uppercase, lowercase and numbers

I am new to regular expression. 我是新来的正则表达式。

I have written one for uppercase, lowercase, 10 characters (min) and a number. 我写了一个大写,小写,10个字符(最小)和一个数字。

/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])\S{10,}$/g

This validates correctly. 这可以正确验证。

I was wondering if I could run a check for a minimum of two uppercase chars, two lowercase chars and two numbers? 我想知道是否可以对至少两个大写字符,两个小写字符和两个数字进行检查?

I have tried: 我努力了:

/^(?=.*[A-Z]{2,})(?=.*[a-z]{2,})(?=.*[0-9]{2,})\S{10,}$/g

Cheers 干杯

Yes, it's easy, you only need to repeat twice the whole content of each lookahead (using a non-capturing group) : 是的,这很容易,您只需将每个前瞻的全部内容重复两次(使用非捕获组)

/^(?=(?:.*[A-Z]){2})(?=(?:.*[a-z]){2})(?=(?:.*[0-9]){2})\S{10,}$/

You can improve the pattern using negated character classes: 您可以使用否定的字符类来改善模式:

/^(?=(?:[^A-Z]*[A-Z]){2})(?=(?:[^a-z]*[a-z]){2})(?=(?:[^0-9]*[0-9]){2})\S{10,}$/

Note: since you test all the string with a single pattern anchored at the begining and at the end, you don't need to add "g" for a global research. 注意:由于您在开始和结束时都使用单个模式测试了所有字符串,因此您无需为全局研究添加“ g”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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