RegEx Lookaheads Question [Regular Expressions]

Hi everyone,

I was able to pass the Positive and Negative Lookahead section, but I’m still having issues understanding exactly how it works.

It’s my understanding that lookaheads will return true or false based on whether the string we are testing it against matches the patterns inside the Regex.

In my own JS environment I tested the following Regex:


My understanding would be it would return True, if: the string is between 3 and 6 alphanumeric characters in length and it begins with a number.

I tested the regex against the following values and added their results (True or False):

  • 2abc = true
  • abc1 = false
  • abc12 = false
  • abc123 = true
  • 2abcdefghi = true

I’m wondering why “abc123” returned True, as it does not start with a number (?=\d).
And why 2abcdefghi returned True, when it is longer than 6 characters.


you are missing a thing: the test method will return true if it finds the pattern inside the string.

abc123 = true
2abcdefghi = true

these two match the pattern because they have the substrings 123 the first, and 2abcde the second

If you want it to return true only if the whole string match the pattern you need to write that explicitly

1 Like

Just to clarify, does this mean as long as there is a pattern that

  1. Starts with a number
  2. Continues for 3 to 6 alphanumeric characters, after that number
    It will return True?

And what would I need to add to explicitly state that the entire string would match need to match the pattern?

yes, exactly

you need to write inside the pattern that it needs to start at the beginning of the string and end at the end
you have met it already in the curriculum, do you remember what’s used for that? if you don’t try to look at the titles of the challenges in that section, maybe one will remind you of something or you find which challenge you need to review
or try to use google

1 Like

I’m assuming it would be (^) for the start and ($) for the end?

I’ve tried:


But I don’t think I’m using it correctly, as its not returning True when it should be

what does this return then? which are the strings that give unwanted results?

1 Like
  • 1abc = False
  • 1abcdef = False
  • 1abcde = False

the end of string symbol should also be at the end of the pattern, as it is not possible to match something after the end of the string

1 Like

Ooooh I see,

So would this be correct?


I’ve tested it and it seems to be working!

I would prefer to write it as /^(?=\w{3,6})(?=\d)$/ but it works all the same

Happy coding!

Thanks for being so patient with me (and for writing such quick responses)!