NobinPegasus
NobinPegasus

Reputation: 787

regex lookahead confusion

let sampleWord = "mA11";
let match1 = /(?=\w{4,6})(?=\D\d{2})/;
let match2 = /(?=\w{4,6})/;
let match3 = /(?=\D\d{2})/;

console.log(match1.test(sampleWord));
console.log(match2.test(sampleWord));
console.log(match3.test(sampleWord));

Here's the console output:

// console output
false
true
true

Here's my regex code. My confusion is the first part /?=\w{4,6}/ This means a string of character 4 to 6. and the second part (?=\D\d{2}) This means anystring that has one non digit character in the beginning and two consecutive digits. so How is 'mA11' not matching /(?=\w{4,6})(?=\D\d{2})/; it's of length 4 and a11 also satisfies the second condition. And how is the 'mA111' string matching the match1 condition? This also satisfies for strings more than length 6 which I don't know how?

Upvotes: 1

Views: 28

Answers (0)

Related Questions