A lookbehind assertion constrains a match by checking the text immediately before the current position. It is zero-width: the preceding text must satisfy the assertion, but it is neither matched nor consumed. section 11.5.3
Require or reject preceding text
Use /(?<=David )BLACK/ to match BLACK only when it follows “David ”. Use /(?<!David )BLACK/ for the opposite condition: match BLACK only when it does not follow “David”.
Distinguish lookbehind from nearby regular-expression constructs:
- Lookahead assertions inspect following text without consuming it; lookbehind assertions perform the equivalent check on preceding text. section 11.5.3
- Non-capturing groups consume matched characters but omit them from numbered captures; lookbehind assertions consume nothing. section 11.5.3
