简体   繁体   English

如何为正则表达式添加多个条件?

[英]How can I add multiple conditions to regex?

I have tried many times but could not find the solution. 我已经尝试了很多次,但是找不到解决方案。

One of the rules is if string ends with digit then it must contain one more digit in string anywhere 规则之一是,如果字符串以数字结尾,那么它必须在字符串中的任意位置再包含一个数字

These are the rules I have - as you can see in the console my test does not work as expected 这些是我的规则-如您在控制台中所见,我的测试无法按预期进行

/* password length 6-80, */
/* if ends with digit then must contain two digit, */
/* 1 upper case character, 1 lowercase character, 1 digit and 1 special character. */
/* If password starts with uppercase character then 2 uppercase character are required. */

 var re = /(?=.*\\d)(?=.*[az])(?=.*[AZ])(?=.*[-_*&^%$#@!~])((?=\\b[AZ]).*[AZ].*[AZ].*|(?!\\b[AZ]))((?=.*\\d$).*\\d.*\\d.*|(?!.*\\d$)){6,80}/g; console.log("false?",re.test('Aa@sasA1')); console.log("true?",re.test('Aa@sa1sA1')); 

 var re = /^(?=.*\\d)(?=.*[az])(?=.*[AZ])(?=.*[-_*&^%$#@!~])(?=[AZ].*[AZ]|[^AZ])(?=.*\\d.*\\d$|.*\\D$).{6,80}$/; console.log("false?",re.test('Aa@sasA1')); console.log("true?",re.test('Aa@sa1sA1')); 

Explanation: 说明:

(?=[A-Z].*[A-Z]|[^A-Z]) : first character: 1 upper and another one OR not upper
(?=.*\d.*\d$|.*\D$)     : last character: digit and another one before OR not a digit

That requirements look weird. 那个要求看起来很奇怪。 Please have a read to Microsoft Password Guidance . 请阅读Microsoft密码指南

Must contain one digit + must contain one digit which is not last character when last is a digit = must contain one digit before last character. 必须包含一个数字+必须包含一个不是最后一个字符的数字,如果last是一个数字=必须在最后一个字符之前包含一个数字。 In other words: replace (?=.*\\d) with (?=.*\\d.) . 换句话说:将(?=.*\\d)替换为(?=.*\\d.)

Almost same for uppercase, true requirement is having an uppercase which is not first character. 对于大写字母几乎相同,真正的要求是具有不是第一个字符的大写字母。

 var re = /^(?=.*\\d.)(?=.*[az])(?=.+[AZ])(?=.*[-_*&^%$#@!~]).{6,80}$/; console.log("false?",re.test('Aa@sasA1')); console.log("false?",re.test('Aa@sasa1')); console.log("true?",re.test('Aa@sa1sA1')); 

We also can add some contrast to prevent unneeded backtracking. 我们还可以添加一些对比,以防止不必要的回溯。 As quantifiers, either greedy or lazy, can lead backtracking, better option is to work around character classes. 由于量词(无论是贪婪的还是懒惰的)都可能导致回溯,因此更好的选择是解决字符类。

Considering first lookahead we need to match a digit anywhere before the last character. 考虑到先行搜索,我们需要在最后一个字符之前的任何位置匹配一个数字。 (?=.*\\d.) then becomes (?=\\D*\\d.) , matching non digits first preventing backtracking. (?=.*\\d.)然后变为(?=\\D*\\d.) ,首先匹配非数字以防止回溯。

Following that principle, the whole regex becomes: 遵循该原则,整个正则表达式变为:

^(?=\D*\d.)(?=[^a-z]*[a-z])(?=.[^A-Z]*[A-Z])(?=[^-_*&^%$#@!~]*[-_*&^%$#@!~]).{6,80}$

That way, when user enters a particularly long string, failing will occur fast (have a look to The elements of Regex Style ). 这样,当用户输入特别长的字符串时,失败很快就会发生(请看一下Regex Style的元素 )。

 var re = /^(?=\\D*\\d.)(?=[^az]*[az])(?=.[^AZ]*[AZ])(?=[^-_*&^%$#@!~]*[-_*&^%$#@!~]).{6,80}$/; console.log("false?",re.test('Aa@sasA1')); console.log("false?",re.test('Aa@sasa1')); console.log("true?",re.test('Aa@sa1sA1')); 

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

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