I am doing positive and negative lookaheads and I can’t understand the concept in itself…But still I tried my hands on this and it is not passing one test case…The code I did so far is given below
let sampleWord = "astronaut";
let pwRegex = /(?=\w)(?=\d{2} *){5,}/; // Change this line
let result = pwRegex.test(sampleWord);
The following test case did not pass:
Your regex should not match the string
12345
i) can anyone explain what is positive and negative lookaheads in simple words
ii)what is the mistake i have made in my code??
What are the other test cases? That would be helpful to find out what your objective is.
Lookahead/behind makes the engine to parse the string but to stay at the last position. Weird concept; only useful imo for “find something not followed by something else” (negative lookahead).
Practically, the engine parses the string until it finds the first match of the regEx-“rule”. Then it kind of “locks the position”, but keeps looking (ahead|behind), continuing trying to match its pattern against the string. Depending on your lookahead/behind-construct, it will evaluate positive and continue or negative and abort. If it evaluates positively, it will continue string evaluation from the previously locked position.