Module 3 · Lesson 1 of 49/32 in the course~15 min
Module lessons (1/4)
Start and end: `^` and `$`
Lesson preview5 hands-on exercises readyRegex Course · Module 3 — Anchors and boundaries
Anchoring a pattern to the first or last character of the string.
What you'll learn in this lesson
- Understand that
^and$are zero-width assertions: they check the position without consuming characters. - Use the
^pattern$pair to validate the entire string and avoid partial matches on unwanted prefixes or suffixes. - Distinguish between
^still leading (outside[...]) and^negation operator (inside[^...]). - Combine
^/$con quantificatori esatti (^\d{4}$,^\d+$) for robust form controls.
Overview
The anchors do not represent text characters, but position boundaries (zero-width assertions). The two fundamental ones are ^ (absolute start of the string) and $ (absolute end of the string, without flag m). Paired together, they transform a generic substring search into a rigorous validation of the entire input.
Sections covered in this lesson
- 1Mental model: free search vs anchored validation
- 2Progressive examples
- 3Guided laboratory
- 4Check for understanding
Pro Lesson · Module 3 — Anchors and boundaries
Unlock "Start and end: `^` and `$`" and the full Regex Course curriculum
Anchoring a pattern to the first or last character of the string.
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.