Skip to main content
Module 3 · Lesson 2 of 410/32 in the course~16 min
Module lessons (2/4)

Word boundaries: `\b` `\B`

Lesson preview5 hands-on exercises readyRegex Course · Module 3 — Anchors and boundaries

The transition between word and non-word characters; finding whole words.

What you'll learn in this lesson

  • Isolate complete words or integers using \b (word boundary) without consuming surrounding spaces.
  • Distinguish between boundary \b (word/non-word transition) and non-boundary \B (interior of a word).
  • Avoid accidental matches within compound words (cat within category, 42 within var42).
  • Recognize the ASCII limits of \b on Italian accented words and know when Unicode lookarounds are needed.

Overview

\b is a word boundary anchor: checks for a zero-width position where one side is a word character (\w = [A-Za-z0-9_]) and the other side is NOT a word character (\W, or start/end of string). In contrast, \B matches any position that is NOT a word boundary (for example within a word or between two spaces).

Sections covered in this lesson

  1. 1Mental model: boundary \b vs non-boundary \B
  2. 2Progressive examples
  3. 3Guided laboratory
  4. 4Check for understanding
Pro Lesson · Module 3 — Anchors and boundaries

Unlock "Word boundaries: `\b` `\B`" and the full Regex Course curriculum

The transition between word and non-word characters; finding whole words.

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.