Hi All
Hope all is well
I have a question regarding the $ character in regexs
In the following expression, why doesn’t it match the pattern below?
let myRegex = /^(a)(?!b)$/;
let myString = "an";
result = test.myRegex(myString); // returns false
The way I understand the regex is:
- Match first capture group if it is not followed by the pattern in negative lookahead
- Ensure first capture group is at the beginning of the pattern
- Whatever doesnt match the negative lookahead must be at the end of the pattern
But what appears to be happening is the pattern inside the negative lookahead is ignored by the $ boundary assertion entirely, and it only sees the first capture group, so when there are more patterns than allowed for in the first capture group it fails to match the entire pattern.
Is this understanding correct? do end of pattern assertions not work on negative lookaheads?
Thanks for taking the time to read this!
D.W.
EDIT:
On the other hand, inserting the boundary assertion inside of the negative lookahead does make this work; which is a curious result to me…
let myRegex = /^(a)(?!b$)/; // returns true when tested against \"am\"