Need help with positive and negative lookaheads

Need help with positive and negative lookaheads
0

#1

Tell us what’s happening:

Your code so far


let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,}(?=\d\d*))/; // Change this line
let result = pwRegex.test(sampleWord);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead


#2

i think you’re missing a left bracket after the left curly brace
try this
let pwRegex = /(?=\w{5,})(?=\d\d*)/; // Change this line

Edit: fixed the brackets on the far left also


#3

Let me give you a deeper explanation of how it works. Look at it as a condition for you to extract data. That is,

  1. For a positive lookaheads, you’ll extract data, it the string contains some form of expression you have stated.
regEx_to_extract_data(?= regEx_that_matches_data_of_your_condition)

// e.g const str = stew;
const regEx = /s(?=w)/;

console.log(str.match(regEx));

// Required output: [ s ]

Meaning, if you look ahead in the string “stew” and you find “w”, return s

The negative lookahead is just the reverse of this.
Hope this helps


#4

I changed up my code and i got this :
let sampleWord = “astronaut”;
let pwRegex = /(?=\w{5,})(?=\d\d*)/; // Change this line
let result = pwRegex.test(sampleWord);
But it is still incorrect, I don’t know what’s wrong.


#5

Change your regEx to /(?=\w{5,})(?=\D*\d\d)/


#6

Thank you very much, I think I understand now.