Positive and negative lookahead - passing all but one


After searching and reading I’m still struggling with this activity. I finally looked at the answer, but I don’t want to just pass it. I want to understand it.

My regex is matching everything except for "astr1on11aut"

This is what I’ve written: /^(?=\w{5,})(?=\D+\d{2,})/

I’m not sure why I’m failing it. Also, what is the \D+ doing exactly? Why do I need to check for non-digit characters? Is that to make sure the first character is not a digit?

Thanks for helping me!

yes, the first character should not be a digit so you need to check that

the issue is there but it’s an other, your pattern says “one or more non digit charavter followed by at least two digits”, but instead you should not make the limit that the two consecutive numbers should be the first numbers in the string

numbers can be anywhere in the string but first position

1 Like

Okay so the \d{2,} really needs to be \d{2}. The comma is telling it that the digits must be consecutive correct?

I’m trying to make sure I understand the order of operations here:

In the answer: /^\D(?=\w{5,})(?=\w+\d{2})/

  1. So first ^\D Checks to make sure the first character is not a digit
  2. Then (?=\w{5,}) checks to make sure that it is atleast five characters long
  3. Finally, `(?=\w+\d{2}) checks one or more alpha characters plus at least two digits

Am I understanding the sequence correctly?

Thanks for your help :slight_smile:

as you do not have a end of string character there, and there is nothing else in the pattern after the two consecutive digits, there is no difference in this case between \d{2} and \d{2,}, maybe the first one it’s a bit less resource intensive, but in terms of results there is no result

that’s the order in which they are evaluated, but it’s important to get that both lookaheads start looking from the same position