Unable to match 2 consecutive numbers, regex

Tell us what’s happening:
Hello all,

Need help figuring out why my code is not passing the last test:

Your regex should match "astr1on11aut"

First i use ^[\D] - so the it can´t start with a number.

then i use (?=[\w{6,}]) - so we ensure it is least 6 characters long

and lastly where my problem probably is (?=\D*\d{2,}) - trying to check for 2 or more consecutive numbers, but i want it to so it doesnt matter if it´s at the start end or middle of the string, just check anywhere if we have 2 numbers together.

Can someone give me a hint please?

Thank you


Your code so far

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

// greater than 5 characters long, CHECK
// do not begin with numbers, CHECK
// have two consecutive digits, CHECK

// use 2 positive lookaheads, CHECK

Challenge: Positive and Negative Lookahead

Challenge Link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

1 Like

Not allowed to use links thats why i had to delete it

1 Like

your issue is exactly this (one of them), you are saying “zero or more non numbers followed by two consecutive numbers”, that means that you are excluding any number before the two consecutive ones. Maybe you need to use a different symbol.

also note that you can’t use {6,} inside a character class (inside square parenthesis), it interprets it as you wanting to match those characters literally, and that also can make it fail, plus you don’t have the length limit here anymore
here the breakdown of your regular expression using https://regex101.com/


Thank you, i changed from \D* to \w* and it worked, it was like you said, ignoring any previous numbers before my 2 consecutive numbers.

as for the other thing you mentioned {6,}, in my case i wasnt using this inside any square brackets, do you mean this as a tip to keep in mind, or am i missing something here?

Anyways, your help allowed me to figure out a solution in 10 seconds :stuck_out_tongue:

One more thing if you don´t mind.

Why would something like this work?


my regex doesnt mention anything about ending with letters?

it mentions it can have any character before 2 consecutive digits, but doesnt mention it can have anything else after these digits?

This is confusing sorry.

you totally have it inside square brackets.

there are three conditions:

  • greater than 5 characters long,
  • do not begin with numbers,
  • have two consecutive digits.

None of this says anything about how the password should end.

You will have something more complex in the challenge you will find later on, " Restrict Possible Usernames", which will also deal with contricting how the string should end

1 Like

Thank you makes more sense now, will have a look at this {} thing inside brackets.

the square brackets will make a character class, so it means that it will be matched any of the things inside them, with the graph parenthesis you make a counter, saying how many times a thing should repeat to have a match - you can’t have a counter inside the square parenthesis, it will not work, instead it will add the graph parenthesis to the possible matches. you can do for example [abc]{3}, and that will match any of the characters inside the square parenthesis three consecutive times, so for example “aaa”, “aab”, “abc”, “bcc” etc… you can’t do [abc{3}], that will match a single character between any that are inside the square parenthesis

1 Like