Level 2: Practical patterns · Lesson 12 of 30
Alternation |
The pipe | means or. It lets a pattern match one of several alternatives, and groups control how far each alternative reaches.
The pipe character | separates alternatives. The pattern cat|dog matches either cat or dog. You can list as many options as you like: red|green|blue matches any of the three colour words. Alternation is how regex says or.
The pipe has the lowest precedence of any regex operator. That means it splits the entire pattern, or the entire group it sits in, into pieces. black|white cat does not mean black cat or white cat. It means black on its own, or white cat. This is the most common alternation mistake.
Use a group to limit the alternatives. (black|white) cat matches black cat and white cat. The group says that only the colour word varies, and the text cat is shared. If you do not need to capture the colour, write (?:black|white) cat instead.
The order of alternatives matters. The engine tries them from left to right and takes the first one that works at the current position, not the longest. So in|inside on the text inside matches only in. Put longer alternatives first, as in inside|in, or add word boundaries: \b(?:in|inside)\b forces the engine to keep trying until the whole word matches.
Alternation works the same in JavaScript, Python, PHP, Java, C# and Ruby, which all use backtracking engines that pick the first alternative that leads to an overall match. Go and Rust use engines that guarantee linear time; they still return leftmost matches and follow the same leftmost-first preference for alternatives in their standard APIs, so the ordering advice holds there too.
For single characters, a character set is usually better than alternation. [aeiou] is clearer and faster than a|e|i|o|u. Save alternation for choices between words or longer chunks, such as (?:jpg|jpeg|png) for file extensions or (?:USD|EUR) for currency codes.
Combine alternation with anchors carefully. ^cat|dog$ means cat at the start, or dog at the end. If you want the whole text to be either word, write ^(?:cat|dog)$. Once again, a group fixes the precedence.
Live tester
3 matches (JavaScript engine)
cat dog bird catalog
Examples
Numbers
/\b(?:10|20)\b/g10 15 20 200
Matches: “10”, “20”
The group limits the alternatives, and boundaries stop 20 from matching inside 200.
Text
/red|blue/gred green blue
Matches: “red”, “blue”
Each alternative is a complete word, so green is skipped.
Codes
/\b(?:USD|EUR)\d+/gUSD100 EUR50 GBP20
Matches: “USD100”, “EUR50”
Only the listed currency prefixes are accepted before the digits.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match the pets cat and dog, but not the other animals.
cat dog bird fish
Task 2: Match the whole words inside and into, without stopping early at in.
in inside index into
Task 3: Match black cat and white cat as full phrases.
black cat, white cat, black dog, brown cat