如何使用以下条件进行验证?

How to make validation with the following conditions?

用户名:至少 5 个字母且全部小写 密码 : 是2位数字后跟符号“@”或符号“&”再后跟4个大写字母的组合

示例: 用户名:丸子/jungernaut 密码 : 21@YOUR / 74&GOOD

我试试

var slt = "gordonbam"
var sltk = "12@BANA"
var username =  /[1-5][A-Z]/g;
var password = /[1-2](@|&)\w[1-4][A-Z]/g;

var result1 = slt.match(username);
var result2 = sltk.match(password);

console.log(result1);
console.log(result2);

也许,

^[0-9]{2}[@&][A-Z]{4}$

^[a-z]{5,}$

对于通行证和用户名可能工作正常。


如果您希望 simplify/modify/explore 表达式,regex101.com. If you'd like, you can also watch in this link 的右上面板已对其进行说明,它将如何与一些示例输入相匹配。


正则表达式电路

jex.im 可视化正则表达式:

const regex = /^\d{2}[@&][A-Z]{4}$/gm;
const str = `21@YOUR
74&GOOD
74&aaaa
74&AAAz`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

用户名:

^([a-z]*[a-z]){5}[a-z]*$

密码:

^[0-9]{2}[@][A-Z]{4}$

试试这个,如果有任何问题,请评论我