Search code examples
regexregex-lookaroundsregex-groupregex-greedy

RegEx for Positive Lookahead - password rules (contains required numbers and characters)


I'd like to understand the below regex and tried testing it in regex101

^(?=.*[a-zA-Z])(?=.*[0-9]).{4,}$

Solution

  • That regex doesn't mean much and can be shortened to:

    .{4,}$ // match at least 4 characters (or more) before ending
    

    The reason is that lookaheads define where your matching group pattern ends. But you are placing the lookahead at the start of the input string, catching "" (nothing) in front of all the lookahead patterns. So all the lookaheads are redundant.

    So:

    ^ pattern must start at the beginning of input

    (?=.*[a-zA-Z]) find lookahead of any number of consecutive alphabets (found "TestPassword", not to be included in matching group)

    (?=.*[0-9]) find lookahead of any number of digits (found "1", not to be included in matching group)

    Given above, the only match is "" at the beginning of "TestPassword1". Now we continue the matching...

    .{4,}$ now match anything of at least 4 characters situated right at the end of input (found "TestPassword1", which is returned as matching group)

    See below code for proof and explanation:

    let regex = /^(?=.*[a-zA-Z])(?=.*[0-9]).{4,}$/;
    [match] = "TestPassword1".match(regex);
    console.log(match); //TestPassword1
    
    // just test for lookaheads result in matching an empty string at the start of input (before "T")
    regex = /^(?=.*[a-zA-Z])(?=.*[0-9])/;
    match = "TestPassword1".match(regex);
    console.log(match); //[""]
    
    // we're now testing for at least 4 characters of anything just before the end of input
    regex = /.{4,}$/;
    [match] = "TestPassword1".match(regex);
    console.log(match); //TestPassword1