Error in a regular expression

Tell us what’s happening:
I don’t get this code to work. It should detect an error only if there it starts with a number, has two consecutives numbers and should be at least 5 characters long. But it detects also if there is a number alone
like “D9a99aa”

Your code so far
let sampleWord = “D9a99aa”;
let pwRegex = /(?=\w{5,})^(?!\d)(?=\D*\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:

Have you tried regular expressions 101? It is a great resource that lets you quickly iterate over your regex and it provides a great explanation on what each field does.

3 Likes

This is an amazing resource. Thank you for sharing! Definitely holding on to this one.

1 Like

Its my pleasure. :slight_smile:

I think that website is a gift to all programmers. It has made working with regular expressions so much easier for me over the years.

It’s a very good resource, I don’t complain. I finally got the mistake, but I still have to think more on where was the problem

1 Like

I’m glad to hear you were able to figure it out. Nice job!

1 Like