A negative lookbehind lets a regular expression match only when its pattern does not match the text immediately before the current position. It checks right to left from that position; it does not search the entire preceding string unless the pattern is written to do so. section 12.6.2
Listing 12.12 Using a negative lookbehind to check for a preceding word
const realStrikeRegex = /(?<!not\s+)strike/gi; #A
const orders =
"Strike at dawn, but do not strike at dusk";
const shouted = orders.replace( #B
realStrikeRegex, #B
(command) => command.toUpperCase(), #B
); #B
console.assert(
shouted ===
"STRIKE at dawn, but do not strike at dusk",
"Only the order that is not negated is shouted",
);
Here, (?<!not\s+) excludes “strike” when preceded by “not” and whitespace. The case-insensitive, global replacement uppercases the unnegated order. section 12.6.2
