Regex Tutorial: How to Test Regular Expressions
Learn the regex building blocks you actually use — character classes, quantifiers, groups and flags — and test patterns live with highlighted matches.
Regular expressions find, validate and replace text by pattern: every email in a document, every date in a log, every price in a page. They look cryptic, but most everyday patterns use a small set of building blocks.
The building blocks
\da digit,\wa letter, digit or underscore,\swhitespace,.any character.[abc]one of a, b or c;[^abc]anything except them;[a-z]a range.- Quantifiers:
*zero or more,+one or more,?optional,{3}exactly three,{2,4}two to four. - Anchors:
^start and$end of the string (or line with the m flag),\ba word boundary. - Groups:
( )captures,(?: )groups without capturing,(?<year> )a named group.
Examples
- Date like 2026-10-20:
\d{4}-\d{2}-\d{2} - Hex color:
#[0-9a-fA-F]{6}\b - Capture the year, month and day:
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
How to test a regex
- Open the Regex Tester.
- Type your pattern and paste a test string — matches are highlighted as you type, and capture groups and named groups are listed for each match.
- Toggle flags: g (all matches), i (ignore case), m (multiline), s (dot matches newlines), u (Unicode), y (sticky).
- Preview a replacement using
$1,$&or$<name>.
Presets for emails, URLs, phone numbers, dates, hex colors and IPv4 addresses give you a starting point. Patterns run in an isolated worker, so a pattern with catastrophic backtracking is stopped instead of freezing the page.
Common mistakes
- Forgetting to escape special characters: a literal dot is
\.. - Greedy matching:
<.*>matches from the first < to the last >; use<.*?>for the shortest match. - Missing the g flag when you expect every match, not just the first.
Frequently Asked Questions
Which regex flavor does the tester use?
JavaScript (ECMAScript) regular expressions, exactly as your browser runs them, including named groups, lookbehind and Unicode property escapes.
What does the g flag do?
It finds all matches in the text instead of stopping at the first one.
How do I match a literal dot or question mark?
Escape it with a backslash: \. matches a dot and \? matches a question mark.
Why is my regex slow or freezing?
Nested quantifiers like (a+)+ can cause catastrophic backtracking. The tester stops such patterns; rewrite them to be more specific.
