Alternation |
The pipe | means or: cat|dog matches cat or dog. Alternation has the lowest precedence, so ^cat|dog$ means (start then cat) or (dog then end); wrap it in a group, ^(?:cat|dog)$, to anchor both sides. The engine tries the options left to right and keeps the first that lets the rest of the pattern succeed.
Syntax
a|b|c (?:a|b)c ^(?:a|b)$Examples
"I have a cat, a dog and a bird".match(/cat|dog|bird/g)Result: ["cat", "dog", "bird"]
["cat", "dog", "catalog"].map(s => /^(?:cat|dog)$/.test(s))Result: [true, true, false]
The group makes both anchors apply to every alternative.
"10 km, 5 mi, 3 m".match(/\d+ (?:km|mi|m)\b/g)Result: ["10 km", "5 mi", "3 m"]
Longer units listed before the one-letter unit.
"Mr Smith, Mrs Jones, Dr Who".match(/\b(?:Mr|Mrs|Ms|Dr)\b/g)Result: ["Mr", "Mrs", "Dr"]
The trailing \b forces Mrs to be matched whole rather than as Mr.
Common mistakes
- Forgetting the group: ^cat|dog$ accepts "cat food" and "hot dog".
- Listing a prefix first: Mr|Mrs matches Mr inside Mrs; put longer options first or add \b.
- Writing [cat|dog], which is a character class of the letters c a t | d o g.
- Using a very long alternation of literals in a hot loop; match a broader shape first and look the word up in a Set.
Practice Alternation |
These drills check your answer on live data, free, no account needed.
- Alternation: cat, dog or birdThe whole line must match. Match exactly one of the words cat, dog or bird.
- Alternation with anchorsA match anywhere counts, so anchor it yourself. Match lines that are exactly yes or exactly no.
- Grouped alternatives: Mr, Mrs, Ms, DrThe whole line must match. Match a title (Mr, Mrs, Ms or Dr), an optional period, one space, and a capitalized name with at least one lowercase letter after the capital.
Questions
How do I match either of two words in regex?
word1|word2, usually wrapped and bounded: \b(?:word1|word2)\b so that partial words do not match.
Can I use or inside a character class?
A character class is already an or between single characters: [abc] means a or b or c. Use | only for sequences longer than one character.
Does the order of alternatives matter?
Yes. The engine takes the first alternative that lets the whole pattern succeed, so for overlapping options like a|ab the shorter one wins unless something after it fails.
Related
- Capture groupsHow parentheses capture text, reading groups in JavaScript via match, matchAll and exec, reusing them with $1 in replace, and the g trap that loses groups.
- Non-capturing group (?:...)How (?:...) groups for quantifiers and alternation without a numbered capture, why that keeps group numbers stable, and how it changes findall in Python.
- Anchors ^ and $What ^ and $ match, how they change with the m flag, whole-string versus per-line matching, and the mistakes with alternation and trailing newlines.
- Optional with ?How ? makes a character optional, making a whole sequence optional with a group, what the capture holds when the group is absent, and the ? that means lazy.