Regex Explainer

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

Alternation binds more loosely than everything else, so the anchor applies to only one branch.

/^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.

What to check

  • Probably a bug

    Unanchored — matches anywhere in the input

    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.

Step by step

  1. 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.

Details

Capture groups
None
Flags
None set

Check your own pattern

Paste one on the home page, or call the API or MCP server. This page is also available as markdown.

Related examples