How to Test a Regular Expression

Learn how regular expressions work, why patterns often fail to match, how flags and capture groups behave, and how to test a regex against sample text.

Regular expressions are powerful but notoriously fiddly. A tiny mistake can silently match nothing or match far too much. This guide covers the essentials for testing and debugging them.

How a regex engine matches

A pattern is matched left to right against the text, and many characters — like ., (, [ and * — are metacharacters that mean something other than the literal character. To match a literal metacharacter you must escape it with a backslash.

Flags change behavior

The g (global) flag makes a pattern find every match rather than stopping at the first, i (ignore case) makes it case-insensitive, and m (multiline) changes how ^ and $ behave. A missing flag is a very common reason a pattern appears to match nothing or only once.

Capture groups

Parentheses capture the text they match into numbered groups, and named groups use the syntax (?<name>...). Seeing each group listed separately is the fastest way to confirm exactly which part of the match each set of parentheses caught.

Debugging a pattern

If a pattern matches nothing, check for a missing flag, an unescaped metacharacter, or an assumption about newlines. If it matches too much, the culprit is usually a greedy quantifier such as * or + that should be made lazy with *? or +?.

Related tools

Regex Tester — Test regular expressions against sample text
Slugify — Convert text into a URL-safe slug
Case Converter — Convert text between camelCase, snake_case and more

All guides