Still can’t wrap my head around this, even after using anchors, not using anchors and using different techniques. I know what is happening and mostly how it works but idk
let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d{2,})/;
let result = pwRegex.test(sampleWord);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.
It doesn’t work
I tried it on both cases, it only needs it on the pattern matching one though I assume. Can you try to use it on your browser with my code and see if it works?
Correct digits should not be at the beginning. Your code is written incorrectly. You need to check for two consecutive digits.
\D* matches any string that contains zero or more occurrences of D .
{2,} two or more times
{5,} five or more times
{2} exactly two times
\w word character
I’m still trying to fully grasp how this works,
so : At the beginning there was God created the heaven and earth…
nah jk
At the beginning check for 2 or more non-numbers,followed by as many (zero or more) alphanumeric characters and atleast 2 numbers. But then I thought that I can check for just one or more as well so this works too:
let pwRegex = /(?=\w{5,})(?=^\D+\w*\d{2})/;
I guess I forgot to add what the regex should check in-between the \D+ and \d{2}…
Thank you for the help!
Your regex should not match "8pass99"
Your regex should not match "12abcde"
let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d{2,})/;
let result = pwRegex.test(sampleWord);
I have tried changing the non-digit \D* to \D+:
/(?=\w{5,})(?=\D+\d{2,})/;
Then it only fails one test, which is the 8Pass99 one. I think that it’s the fact that this current code is allowing numbers to go at the beginning but when I try to use ^ to specify numbers only at the beginning, it fails the “astr1on11aut” test which I think is because that ^ pushes the numbers to the end so it won’t allow the 1 in the middle of that test case. I have tried using both anchor methods as well.
Any suggestions,
Thanks! Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.
you have “one or more non numbers followed by two numbers”
instead there should be no limitt about other numbers, only that there should be two consecutive numbers (and no starting with a number)
can you make this less restrictive while keeping the “two consecutive numbers” thing?
Maybe it would be helpful to use a testing tool like https://regex101.com/ it tries to explain what you type. Remember to choose you language first under the hamburger icon.