I need a RegEx Guru!

Can anybody explain why my regex does’t work in the example below?
image

I tried modifying it and reached the desired effect but I still don’t get why the regex in the first picture doesn’t do the same as the second one?
image

There is an implied and between those two look arounds. Your first regular expression is trying to find a place in that string where it is immediately followed by text that will satisfy both of those look around

1 Like

@TomSssM, in the first example you don’t really do a match at all. You basically do 2 lookaheads:

  1. Check if the string consists of at least 5 \w characters: returns true
  2. Check if the string consists of at least 2 \d characters: returns false as there are not only \d characters in the string

I would do something like this here:

const re = /(?=\w{5,})(^\w*\d{2,}\w*$)/;

…without second lookahead, so I have not only true or false but the actual match itself.

1 Like

Uhm…so, you do not have a lookahead of a lookahead but a single lookahead condition composed by those two? :open_mouth:

It means that the \D* could be replaced in this case by a \w{3} for example right?

But i have one doubt:

x(?=y) Matches x only if x is followed by y .

For example, / Jack(?=Sprat)/ matches “Jack” only if it is followed by “Sprat”.
/Jack(?=Sprat|Frost)/ matches “Jack” only if it is followed by “Sprat” or “Frost”. However, neither “Sprat” nor “Frost” is part of the match results.

The above is taken from MDN (MDN: RegExp).

Why all the string match if it only satisfy those lookahead conditions ( which is told they wouldn’t be part of the match result)? It’s just a js related behaviour and the OP test is more general?

1 Like

I get it now) Thanks) I also tried doing the same except with \D instead of \w

I am not sure if I know 100% why. The way I see this is like an if statement in if’s because there is no other character but lookarounds.
For example, if you write /10(?=px)/ it will only match 10 if it is immediately followed by"px".
But!!! If you write /(?=px)/. There is nothing before the lookahead. So it starts checking the condition starting from the beginning of the string. So the regex /(?=px)/ will match the entire string so long as it begins with “px”. See, because we didn’t put any characters before the lookahead. Try thinking this way about the regexses that others posted in this discussion.
Hope this clarifies things a little bit)
Also, keep in mind that you should not use global flag in this case (as it might match infinity if I understand it right) and that it only matches the entire string or not unlike most other regexses.
Please, someone correct me if I am wrong or said smth incorrectly.

1 Like

Oh my God, I just now, for the first time, realized that astron22aut passes only because it ends in “22aut”. Thank you so much sir)))

1 Like

Little late on this.
A sequence of consecutive look arounds operate at the same character position.
The two lookaheads therefore overlap.
Since \d is a subset of \w, they can be combined as such:

(?=\d{2}\w{3})

which is the factored version and is much faster.
This checks a total of 5 required characters.
There is no need to check for any other optional characters that
the {5,} specifies.