Zero-Width Assertion
A regex construct that tests a condition at the current position without consuming characters. The category includes anchors like ^, $, and \b, plus the four lookaround forms. Zero-width matches require special handling in replace-all loops to avoid infinite iteration.
A zero-width assertion is a regex construct that tests whether a condition holds at the current position in the input without consuming any characters. When the assertion succeeds, the match pointer stays exactly where it was; when it fails, the engine backtracks like any other failed sub-pattern. The category generalizes the more familiar anchors: `^` and `$` assert position relative to a line, `\A` and `\z` relative to the whole input, and `\b` and `\B` assert word boundaries. All of these match a position rather than a character, which is why they have zero width. Regex Lookaround: Lookahead and Lookbehind Explained extends the idea from fixed anchors to arbitrary sub-patterns. Positive lookahead `(?=...)` asserts that the following text matches; negative lookahead `(?!...)` that it does not; positive `(?<=...)` and negative `(?<!...)` lookbehind do the same in the reverse direction. Because the inspected text is never added to the match, lookarounds are the canonical way to express "X but only in context Y" without polluting the captured result. Zero-width matches have a practical gotcha: a pattern that matches the empty string can loop forever in naive replace-all implementations. Mature engines detect this by advancing one character after a zero-width match before continuing the scan. The concept also underlies tokenizers built with `\b` and split functions that use lookarounds to keep delimiters in the output. Zero-width assertions appear in PCRE, Java, Python, JavaScript, .NET, and Ruby, though specific syntax for non-anchor variants differs and some engines (notably RE2) omit lookarounds while keeping anchors.