# Why ^a|b does not mean what it looks like

`/^cat|dog$/`

**Match the start of the input, then "c", then "a", then "t" or "d", then "o", then "g", then the end of the input.**

## Step by step

- `alternation` — Match one of: the start of the input, then "c", then "a", then "t" or "d", then "o", then "g", then the end of the input.

## Warnings

### Unanchored — matches anywhere in the input (caution)

Without ^ and $ the pattern succeeds if it matches ANY substring. A validator meant to check a whole value will therefore accept anything that merely contains a valid value: an email pattern without anchors accepts "nonsense a@b.co nonsense".

**Fix:** Wrap the pattern in ^ and $ if it is validating a whole string. Leave it unanchored only if you are genuinely searching within text.

## Details

- Capture groups: none

---

Canonical URL: https://regex-explainer.gumballtools.com/regex/alternation-precedence-trap
JSON API: `GET https://regex-explainer.gumballtools.com/api/v1/explain?pattern=...`
MCP endpoint: `https://regex-explainer.gumballtools.com/api/mcp`
