liveBook · Manning

Alternation

As explained in Secrets of the JavaScript Ninja, Third Edition

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

Alternation uses the pipe character (|) to match one of several alternatives. Parentheses can group alternatives so another operator applies to the group. section 12.2.2

Alternatives in a repeated group

The pattern /(ab|cd)+/ matches ab, cd, abcd, and cdab, but not acbd: each repetition must match either ab or cd. section 12.2.2

How Secrets of the JavaScript Ninja, Third Edition covers it

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.
How it connects
Combines with grouping and repetition operators in regular expressions.

Also explained in

Compare how 2 books explain alternation →

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.