Skip to main content
Module 6 · Lesson 3 of 423/32 in the course~18 min
Module lessons (3/4)

Lookbehind: `(?<=...)` `(?<!...)`

Lesson preview5 hands-on exercises readyRegex Course · Module 6 — Lookaround

The "look-backward" versions of lookaround; ES2018+ support.

What you'll learn in this lesson

  • Write positive lookbehind (?<=...) and negative (?<!...) to inspect what precedes the zero-width match.
  • Extract numbers, values or identifiers without capturing prefixes such as $, - or protocol prefixes.
  • Learn about modern support for variable-length lookbehinds in JavaScript ES2018+.

Overview

The lookbehind are the exact mirror of the lookahead: instead of looking forward with respect to the cursor, they check at zero width what is immediately before the current position, without including that prefix in the returned match.

Before starting the actual match, the engine checks backwards whether a certain prefix exists ((?<=...)) or does NOT exist ((?<!...)). If the condition on the mirror is satisfied, the match starts immediately after the prefix.

Sections covered in this lesson

  1. 1Mental model: Looking in the rearview mirror
  2. 24 progressive examples step by step
  3. 3Guided practical exercises
  4. 4Quick check
Pro Lesson · Module 6 — Lookaround

Unlock "Lookbehind: `(?<=...)` `(?<!...)`" and the full Regex Course curriculum

The "look-backward" versions of lookaround; ES2018+ support.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Regex Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.