Please explain this solution

Hi, can you explain the solution broken down by piece please:

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

I’m particularly not understanding the . and the * in regex.


Challenge: Positive and Negative Lookahead

Link to the challenge:

hi @am93,

here in /^\D(?=\w{5})(?=\w*\d{2})/


means that the string shouldn’t start with a digit, ^ for start and \D for not digits,

now there are two positive lookahead that needs to be matched for every string

first (?=\w{5})

here its says that the characters should be 5 in length and the character can be a valid word character

second (?=\w*\d{2})

here it checks for two consecutive digits 2 in length for whatever the word is

so for a pattern to be matched both the lookahead should be matched like conditions

Hope this helps

1 Like

Hi @am93!

I have added spoiler tags around your code for those who have not worked on this challenge yet.

There is a great tool for regex calls regex 101

It acts like a debugger for your regular expressions.
Copy and paste your regex into the input field and it will output detailed explanations of what each part means.

oh yeah my mistake i mixed it…i will change that SORRY

Thanks. Any thoughts on the . and the *?

The . is not a part of that problem specifically, but I still need help understanding how it works. For some reason the meaning here doesn’t make sense to me. Also would appreciate a better source to check what does what if you have a better option.

. means that any character excluding linebreak (\n)

  • it means the it will match any 0 or more number of characters
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.