Pos/Neg Lookahead Challenge Question

if not specified otherwise, test will return true if there is a substring (a part) of the string that is matched by the pattern

if you want the pattern to match the whole string you need to indicate so with the symbols for start and/or end of string

1 Like

Can anyone please suggest why

fails the test, as it matches “8pass99”

Surely the \D+ should prevent the single number matching at the beginning?

but you need to say that the match should start at the beginning of the string. if you don’t specify it, it doesn’t happen

Thanks. Makes sense. Cheers.

Hi can someone explain why
doesn’t work?

1 Like

you are saying that the first numbers must be the two consecutive numbers with \D*\d{2}, instead numbers are allowed anywhere in the string (well, other rules must still be respected, so almost anywhere)


Yea, so that’s why astr1on11aut didn’t work, so I just have to use \w* instead of \D*, seems pretty obvious now thx

let sampleWord = "astronaut";
let pwRegex = /((?=^\D)(?=\w*\d{2})){5,}/;
let result = pwRegex.test(sampleWord);


  • (?=^\D) -> checks that password starts with character not with number
  • (?=\w*\d{2}) -> specify that it contains the alphanumeric characters with two consecutive digits at the end
  • (){5,}-> checks that the password contains at least 5 characters long.
1 Like

LOL my solution is messy, but it works


You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

Thanks for your understanding.

oh, it’s my first time posting here, I didn’t realize that. Sorry for any inconveniences

thanks, I found that article really helpful too.

1 Like

I tried many solutions but I can’t pass
have two consecutive digits
can anyone please explain how to achieve this. my solution is below:
and it consists of three parts

The first one is ^\D in order to ensure it didn’t start with numbers.

The second is (?=(^\w{5,})) in order to ensure the password is greater than 5 characters long.

The last part I changed it many times but can’t pass have two consecutive digits.

Is my solution is correct if evaluated for the first two parts or not and also how to pass have two consecutive digits?

consider that when you have two lookaheads, they match from the same position. do it you just have a lookahead to search for two consecutive digits, those would be second and third character of the string. you need a lookahead that will look for two digits anywhere in the string, and also allow other digits

As I understand it, when you are using the “{x,y}” you are saying, find whatever I told you in the left of the brackets and if it meets the requirements, from X length and until Y length, extract it. If it exceeds the limits of Y, it does not extract whatever is left after Y, but it will extract the information from X to Y, hence passing the checking.

Still I’m no pro, just a learner like anyone else here and I might be wrong. Hope this helps you out.

thanks mate, took me an hr but within 10mins of looking thru the link I got it. The wording of the concepts were more straightforward to grasp. :grin:

1 Like

HI there. Should it not be {5,} because it says 5 or more characters. why does the solution have {5}
Thanks, appreciate the assistance

let pwRegex = /(?=^[^\d]\w{5})(?=\D*\d{2,})/i; // Change this line

Hi there. this is my regex pattern!!.
I am not able to match "Your regex should match “astr1on11aut” this condition.
Can I know what is wrong ?

let sampleWord = “astronaut”;
let pwRegex = /(?=^[^\d]\w{5})(?=\w*\d{2,}); // Change this line
let result = pwRegex.test(sampleWord);

Tried this…hope this will helpful to some.

1 Like