Alternation
Regular-expression alternation is a construct that matches one of several alternative pattern branches, typically separated by a vertical bar.
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
- The Well-Grounded Rubyist, Fourth Edition
- The Quick Python Book, Fourth Edition
- Regular Expression Puzzles and AI Coding Assistants
What to know first
- regex groupingA regex capturing group is a parenthesized regular-expression subpattern whose matched text is retained for separate access or reference.
- regular expressionA regular expression is a formal pattern notation that describes sets of strings and specifies how text can be matched, searched, or transformed.
Related concepts
- regular expressionuses · A regular expression is a formal pattern notation that describes sets of strings and specifies how text can be matched, searched, or transformed.
- set unionproduces · Set union is the operation that produces a set containing every element belonging to at least one of its operand sets.
- lookbehind assertionuses · A lookbehind assertion is a regular-expression assertion that checks whether a pattern matches before the current position without consuming the checked input.
- logical ORequivalent to · Logical OR is a Boolean disjunction that is true when at least one of its component propositions is true.
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.

