liveBook · Manning

Alternation

Regular-expression alternation is a construct that matches one of several alternative pattern branches, typically separated by a vertical bar.

How 2 Manning books explain it

Secrets of the JavaScript Ninja, Third Edition

Regex alternation is syntax that allows a match to satisfy one of multiple alternative pattern branches.

How it is used
Matches one of several character or grouped-sequence alternatives in regular expressions.
How deep it goes
Defines pipe syntax and demonstrates grouped alternatives under repetition, without detailing evaluation internals.
Examples
Uses /a|b/ and /(ab|cd)+/ to contrast single-character choices with repeated grouped alternatives.
What you can do after
Evaluate whether a string fits a simple alternative or repeated grouped alternatives.

Read how Secrets of the JavaScript Ninja, Third Edition explains alternation →

Secrets of the JavaScript Ninja, Second Edition

A regular-expression construct that matches one of several alternative subpatterns.

How it is used
Uses alternation to express multiple possible matches within a regular-expression pattern.
How deep it goes
Defines the pipe operator and demonstrates grouped alternatives and a newline-matching workaround.
Examples
Uses `/a|b/`, `/(ab)+|(cd)+/`, and `/(?:.|\s)*/` as examples.
What you can do after
Build regular-expression patterns that match either of several alternatives.

Read how Secrets of the JavaScript Ninja, Second Edition explains alternation →

Also covered in

What to know first

Related concepts

Questions about alternation

What is alternation?

Regular-expression alternation is a construct that matches one of several alternative pattern branches, typically separated by a vertical bar.

Which Manning books explain alternation?

Secrets of the JavaScript Ninja, Third Edition and Secrets of the JavaScript Ninja, Second Edition.

What should I know before learning alternation?

Manning books that teach alternation usually introduce regex grouping and regular expression first.

What kinds of alternation are there?

Kinds of alternation include multi-word alternation.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.