gpt4 book ai didi

javascript - 如何向正则表达式添加多个条件?

转载 作者:行者123 更新时间:2023-11-28 17:43:39 25 4
gpt4 key购买 nike

我尝试了很多次,但没有找到解决办法。

规则之一是如果字符串以数字结尾,那么它必须在字符串中的任何位置多包含一位数字

这些是我的规则 - 正如您在控制台中看到的那样,我的测试没有按预期工作

/* 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)(?=.*[a-z])(?=.*[A-Z])(?=.*[-_*&^%$#@!~])((?=\b[A-Z]).*[A-Z].*[A-Z].*|(?!\b[A-Z]))((?=.*\d$).*\d.*\d.*|(?!.*\d$)){6,80}/g;

console.log("false?",re.test('Aa@sasA1'));
console.log("true?",re.test('Aa@sa1sA1'));

最佳答案

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

console.log("false?",re.test('Aa@sasA1'));
console.log("true?",re.test('Aa@sa1sA1'));

说明:

(?=[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

关于javascript - 如何向正则表达式添加多个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47331020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com