regex to match passwords that are greater than 5 characters long and have two consecutive digits

后端 未结 1 1273
野的像风
野的像风 2021-01-24 14:03

Here\'s what I have, can someone please tell me where I am wrong?

相关标签:
1条回答
  • 2021-01-24 14:37

    Use .* before \d{2} since the consecutive digits may occur anywhere in the string. Your current regex should check for two digits to be present at very first.

    let sampleWord = "bana12";
    let pwRegex = /^(?=\w{5})(?=.*\d{2,})/; // Change this line
    console.log( pwRegex.test(sampleWord))

    Note that \w matches only the word characters, so your regex fails if there input string contain 5 non-word characters. So for checking the string length, it's better to use . instead of \w.

    let pwRegex = /^(?=.{5})(?=.*\d{2,})/;
    
    0 讨论(0)
提交回复
热议问题