Regex Lookahead

in the code provide below the console.log is == true , in my understanding it should be false since the regex checks for
\w {3,6}===(123)
non digit \D=== ( " ")
digit \d=== absent in this case
hence the console output according to my understanding should be false

let passwor = "123 brian"; let checkPas = /(?=\w{3,6})(?=\D*\d)/; console.log(checkPas.test(passwor)); /* console output is is TRUE */

Hi , this article helped me alot with regex lookahed : Lookahead and Lookbehind Tutorial—Tips &Tricks
First :

"123 brian"

Interesting part of lookahead, is that your pointer still remain at the beginning.

(?=\w{3,6})  // First lookahead passed.
 123 brian
^-------------------- after first lookhead passed, your pointer still here

Then, your second lookahead check from begining to end again.

(?=\D*\d)  // Second lookahead passed as well, because \D could be NONE or more, then followed with 1 digit.
123 brian   

About quantifier n* you can read more >> JavaScript RegExp Reference

1 Like

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