This is solution proposed for a challenge, and i can't understand why?

here is the challenge link:
the challenge

why did they use \w* in this solution I found in the hint page :

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

thank you.

Are you asking about the \w or the *?

Update: I think you may be asking why the entire thing (\w*) is needed in the first place. I found a previous thread that explains it.

How do lookaheads work with other code?

no when they are used both in this excercice , why they used it?
i gave my own solution and it passed all the tests except the one where it should match the string 12345, my solution was similar to the hint solution except that my solution doesn’t have the \w*, I wonder why I want to check for longest consecutive alphanumeric patters in the string 12345 where I specified in my regex the positive lookahead (?=\w{2,}), you see what I mean?
or I don’t get how regexes work because all the past challenges of js were easy to me except the regular expression section, it’s a weird bunch of lessons. is it just me?

What is your solution exactly? My understanding of (?=\w*\d{2}) :

* means “zero or more”
\w* means “zero or more alphanumeric characters”
\w*\d{2} means “two consecutive numbers with zero or more alphanumeric characters before them”

WIthout the \w* included in \d{2} will only match two numbers at the beginning of the string (e.g. 82elliot) but not if there are any leading letters (e.g. el82liot)

RegEx is hard for me too, I use the website https://regex101.com/ to test and reference.

Also read How do lookaheads work with other code? (as mentioned above) again. It explains the whole solution in detail.

1 Like

See, i though each of the 2 lookaheads look for each half the string, i relused that each lookahead look into all the string and now what you say make sense.
My issue overall with regexs is that i don’t know when parts of the my long regex for example, will it work on half of the string or all the string… I think it’s because i work in the day and code at night my brain powers are insufficient as for when i started to code last year i was unemployed and free.
Anyway think you.

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