Regexp Alternatives Branches
# Regular Expression - Alternation and Branches
### 1. What is Alternation
Alternation refers to using the `|` symbol in regular expressions to represent an "OR" logical relationship. It allows you to match one of multiple possible patterns.
cat|dog
This pattern will match either "cat" or "dog".
### 2. What is Branching
Branching refers to when the regular expression engine encounters an alternation point during the matching process, it will try different matching paths. When one path fails to match, the engine will backtrack and try other possible paths.
* * *
## Principle Analysis
### How the Alternation Operator `|` Works
1. The regular expression engine scans each option separated by `|` from left to right
2. Try to match the first option, if successful then stop
3. If the first option doesn't match, try the second option
4. Continue until a match is found or all options have been tried
### Grouping and Alternation
Alternation is often combined with grouping `()` to limit the scope of alternation:
## Examples
gr(a|e)y
This pattern will match "gray" or "grey".
### Branch Backtracking Mechanism
When the regular expression engine encounters an alternation point:
1. Remember the current position (create a checkpoint)
2. Try the first branch
3. If it fails, backtrack to the checkpoint
4. Try the next branch
5. Repeat until success or all branches have been tried
* * *
## Practice Examples
### Basic Alternation Example
## Examples
// Match multiple date formats
const datePattern =
YouTip