Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 · Syntax and Matching

Anchors and Word Boundaries

Understand regex anchors and boundaries. Learn ^ and $, the m flag for line anchors, and the word boundaries \b and \B, with runnable examples for validation and whole-word search.

Most regex pieces match characters. Anchors and boundaries match positions: a place between characters that has a certain property, such as "the start of the text" or "the edge of a word". They consume nothing, so the match contains no extra characters for them. They are essential for validation and for whole-word search.

Start and end: ^ and $

^ matches at the start of the text and $ at the end. Put both around a pattern and it must match the entire string.

Node.js Anchoring a whole string

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

This is why validation patterns look like ^...$: it says "the whole input is exactly this shape".

In JavaScript, $ without the m flag matches only at the very end of the string. In some other flavors (PCRE, Python, Java) it also matches before a final newline, so ^\d+$ can accept "42\n" there. If that matters, test it in your own language.

Lines: the m flag

By default ^ and $ refer to the whole string. With the m (multiline) flag they also match at the start and end of each line.

Node.js The m flag

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

With m, ^ matches after every line break and $ before every line break. This is the key to processing logs, CSV rows and config files line by line.

Node.js Strip trailing spaces from every line

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Absolute start and end

JavaScript has no \A or \z like some other flavors. If you need "the very start of the whole text" while the m flag is on, use a lookahead or lookbehind trick: (?<![\s\S]) is the start, (?![\s\S]) is the end.

Node.js Start and end of the whole text with m

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Word boundaries: \b and \B

\b matches a position between a word character ([A-Za-z0-9_]) and a non-word character (or the edge of the text). \B is the opposite: a position that is not a boundary.

flowchart LR
  A["c"] --- B["a"] --- C["t"] --- D[" "] --- E["d"] --- F["o"] --- G["g"]
  classDef edge fill:#fde68a,stroke:#b45309

In "cat dog", there are boundaries before c, between t and the space, between the space and d, and after g. Between letters there are none.

Node.js Whole words only

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Reading the examples: \bcat\b demands a boundary on both sides, so it skips concat and category. \bcat only needs one at the start.

Boundaries and non-ASCII letters

JavaScript defines "word character" as ASCII letters, digits and underscore only. A letter like é is a non-word character for \b, even with the u flag.

Node.js The limits of \b

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

When you work with non-English text, build your own boundary with lookaround and Unicode properties (covered in the lookaround and Unicode lessons).

Practical patterns

Node.js Validation and search with anchors

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does ^ match in /^abc/ without any flags?

    Outside a character class, ^ is an anchor for the start of the text (or of each line, with the m flag).
  2. 2. Which pattern matches the word cat only as a standalone word?

    \b on both sides requires a word boundary before and after, which rejects concat and category.
  3. 3. What does the m flag change?

    m (multiline) changes the meaning of ^ and $ only. The s flag is the one that affects the dot.
  4. 4. Is é a word character for \b in JavaScript?

    In JavaScript \w is [A-Za-z0-9_] even with u, so boundaries next to accented letters behave unexpectedly.

Summary

  • Anchors and boundaries match positions and consume no characters.
  • ^ and $ anchor to the string, or to each line with the m flag; wrap validation patterns in ^...$.
  • \b is the edge of a word, \B is the absence of one; use them for whole-word search.
  • \b only knows ASCII word characters; use lookaround with \p{...} for international text.

Next: Alternation and Precedence