liveBook · Manning

Assertion

As explained in Secrets of the JavaScript Ninja, Third Edition

A zero-length assertion is a regular-expression construct that tests a condition at a position without consuming any characters in the match.

A zero-length assertion tests whether a condition holds at a position in a string without consuming characters. Unlike most regex terms, it affects whether a match occurs but contributes no characters to the match. section 12.6

A boundary and a start condition

`/\b/` can match a word boundary, but its match string is empty. In `/^Z/`, `^` asserts the start of the string, while `Z` is included in the match. section 12.6

Lookahead and lookbehind generalize these assertions by testing for a pattern before or after another pattern without consuming the asserted text. section 12.6

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines assertions and illustrates two cases, then mentions lookaround without detailing its mechanics.
Examples
Uses `/\b/` to test a word boundary and `/^Z/` to illustrate a start assertion.
What you can do after
Evaluate whether a regex condition holds without consuming characters in the match.
How it connects
Connects start and end anchors and word boundaries to lookahead and lookbehind.

Also explained in

Compare how 2 books explain assertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.