-1

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

^(?=.*[a-zA-Z])(?=.*[0-9]).{4,}$
Emma
  • 1
  • 9
  • 28
  • 53
greenb
  • 21
  • 3

2 Answers2

0

Explained

 ^                        # BOS
 (?= .* [a-zA-Z] )        # Lookahead, must be a letter
 (?= .* [0-9] )           # Lookahead, must be a number
 .{4,}                    # Any 4 or more characters 
 $                        # EOS
  • I knew about result. but i didn't understand rules of `positive lookahead`. `regex1(?=regex2)` that is first search `regex1` and if exists `regex2`. but `^(?=.*[a-zA-Z])(?=.*[0-9]).{4,}$` ... – greenb May 23 '19 at 03:03
  • No worries, we've all been there and regex is not easy and can't be grasped in one day. Just take baby steps, one step at a time and you'll get better and better. You can start with resources on the web like https://regexone.com. – chatnoir May 23 '19 at 09:09
  • 1
    @greenb - Assertions and other non-consuming constructs, don't actually exist at character positions. They exist _between_ characters. It is a subject I've written about here at SO, but I just can't find the link to it. It's too bad, maybe you could find it, it would help you to forever understand these ghosts. –  May 23 '19 at 20:41
  • @sln Thank you very much. I understood from your comment :) – greenb May 24 '19 at 00:08
0

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
chatnoir
  • 1,785
  • 1
  • 13
  • 16