Can you explain to me this part of the lesson:
A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true
What part of the lesson is giving you trouble specifically? The more information you can provide us on what parts of the regular expression are confusing the better we can help.
Also, there is a great resource called regex 101 where it will test your regular expressions and provide explanations on what each part does. Just make sure to pick the right language before inputting your regex for testing.
There are two things in regexp you need to consider:
Character
Cursor
Just like in any text editor cursor might be positioned just before the character or just after - never on the character itself. If cursor progresses forward it “eats” character checking the pattern.
Look aheads are instructions that tell regexp (cursor) to “look ahead” from it’s current position (index). So two instructions provided in your example can be translated in English like so:
Without eating characters, look ahead and see if there are from 3 to 6 alphanumeric characters \w
Without eating characters, look ahead and see if there is any digit \d followed by 0 or more non-digit characters.
Note that there is no specification of any particular position of these characters or digits, which makes this rule very loose.
And in the example of "abc123" does the lookaheads both scan the string?
for example: does (?=\w{3,6}) scans the whole "abc123", and then (?=\D*\d) comes after and re-scans the "abc123", for checkPass to return true?
let password = "abc11bfaa";
let checkPass = /(?=\w{3,6})(?=\D*\d\d)/;
let result = checkPass.test(password); // Returns true
console.log(result);
The above ‘(naively) simple password checker’ is supposed to look for between 3 and 6 characters and at least one number, but it does return true for “abc11bfaa” although it is more than 6 characters long, is the problem in the code or i didn’t understand well, can someone clarify please?
Does the /(?=\w{3,6})(?=\D*\d\d)/ work like /(?=\w{3,6})OR(?=\D*\d\d)/ i mean that if only one of them is true (i.e. (?=\w{3,6}) or (?=\D*\d\d)) the whole regex (/(?=\w{3,6})(?=\D*\d\d)/) tests true?