Positive and Negative Lookahead not work in banan1

Tell us what’s happening:

I tried several different expressions and finally got it but not working in banan1

Your code so far


let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d+)/; // Change this line
let result = pwRegex.test(sampleWord);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/70.0.3538.77 Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

(?=\w{5,}) means “is followed by at least 5 word characters” (word character refers to letters, digits, and underscore).
(?=\D*\d+) means “is followed by 0 or more non-digits, followed by 1 or more digits”

Check out regex101.com for a handy tool on regular expressions.

1 Like

So what your lookahead says is “find at least five letters, followed by any number of non-numeric, non-whitespace characters, followed by at least one number”. So this would pass banan1, banan-12, all sorts of things.

The first lookahead is exactly right: five or more. But the second one, (?=\D*\d+), has a couple small issues: do you WANT to allow (for example) punctuation? And how do you change that \d+ to handle two or more (hint: look at the FIRST lookahead, you’re already doing five or more!)

1 Like

https:/uploads/default/original/3X/b/d/bdc93689b92214ae39b00d74dbd8650949d73124.png

And banan1 should FAIL, but because your lookahead says “one or more decimal” (that’s what \d+ means), then it PASSES.

How do you change that from “one or more” to “two or more”?

And the regex tester that I’ve used is https://www.regextester.com/?fam=106060 (that’s with a working regex). Mousing over the various bits of the regex itself will tell you what that part of the expression actually means. I like that.

1 Like

thank you sir <3 :smiling_face_with_three_hearts: