By Usama Farooq · Published 5 Oct 2026 · Updated 16 Nov 2026
Regular Expressions: Groups, Flags and Debugging
TL;DR — the short answer
To test a regex, paste your pattern into a tester and type test text — every match highlights instantly with its capture groups. The g flag finds all matches, i ignores case, m makes ^/$ work per line. Invalid patterns show the exact error. Try ToolsOfWeb’s free regex tester, which runs 100% in your browser — nothing is uploaded.
Regular expressions fail silently: a pattern that matches nothing gives you no error, just an empty result. A regex tester removes the guesswork by showing every match live as you type — highlighted in your text, broken out by capture group, with JavaScript’s exact error message when the pattern itself is invalid. This guide covers what regex actually does, the tokens and flags you’ll use every day, common real-world patterns, greedy vs lazy matching, and the debugging routine professionals use.
Try the tool
Open ToolsOfWeb’s Regex Tester for live highlighting, capture groups, flags, and replace preview.
What a regex actually does
A regular expression is a pattern that describes what text should look like, not the text itself. The regex engine reads your text left to right and tries to find every place where the pattern fits. Most characters match themselves: the pattern cat finds the literal text “cat”. A few characters are special — they act as instructions. . means “any single character”, \d means “any digit”, and * means “the previous thing, zero or more times”. So \d+ means “one or more digits in a row”, which matches 5, 42, and 2026.
The mental model that makes regex click: you’re building a template with some slots left blank. The template \d\d:\d\d is “two digits, a colon, two digits” — it matches times like 09:30. Everything else in the text is ignored. This is why regex is so good at validation (“is this a phone number?”), extraction (“pull every email from this text”), and reformatting (“swap day and month in these dates”).
How to test a regex (step-by-step)
- Open the regex tester and type your pattern between the slashes. Watch for the red error indicator — it means the pattern itself is invalid (usually an unclosed bracket).
- Paste your test text — matches highlight yellow instantly. Always include both a positive case (text that should match) and a negative case (text that shouldn’t).
- Toggle flags (start with
g) and watch the match list update. Withoutgyou only see the first match, which hides most problems. - Check the match list for capture-group values and positions. Positions tell you where each match happened — useful when the wrong part of the text is matching.
- Flip to Replace mode to preview substitutions before using them in code. This is where back-reference mistakes show up before they cause damage.
Example: (\w+)@(\w+\.\w+) against Contact hello@toolsofweb.com finds one match with group 1 = hello and group 2 = toolsofweb.com.
The building blocks: tokens you’ll use daily
About twenty tokens cover most real-world regex. Here’s the working set:
| Token | Means | Example match |
|---|---|---|
\d | One digit (0–9) | \d\d:\d\d matches 09:30 |
\w | One word character (letter, digit, _) | \w+ matches hello in “hello world” |
\s | One whitespace (space, tab, newline) | \s+ splits words on whitespace |
. | Any single character except newline | c.t matches cat, cut, c9t |
* + ? | Zero-or-more / one-or-more / zero-or-one of the previous token | https? matches http and https |
{n} {n,m} | Exactly n / between n and m repetitions | \d4 matches a 4-digit year |
[abc] [a-z] | Any one of the listed / ranged characters | [A-Z][a-z]+ matches Hello |
[^abc] | Any character except these | [^@]+ matches everything before @ |
^ $ | Start / end of the string (or line, with m) | ^\d+$ matches only all-digit text |
\b | Word boundary (edge between word and non-word char) | \bcat\b matches “cat”, not “cats” |
| | OR — either side may match | cat|dog matches cat or dog |
() | Capture group — remembers what matched | (\w+)@ remembers the username |
To match a special character literally, escape it with a backslash: \. matches a real dot, \( matches a real parenthesis. This is the #1 source of “my regex matches everything” bugs — an unescaped dot matches far more than a dot.
Common real-world patterns, explained
These are the patterns people test most often. Copy each into the regex tester with the sample text to see exactly how it behaves.
Email (practical, not perfect): [\w.+-]+@[\w-]+\.[\w.]+ against Reach us at hello@toolsofweb.com today matches hello@toolsofweb.com. Breakdown: [\w.+-]+ takes the username (letters, dots, plus, hyphen), @ is literal, [\w-]+ takes the domain, \. is the literal dot, and [\w.]+ takes the TLD. Fully validating every legal email with regex is a rabbit hole — this practical version is what most forms use.
Phone number (flexible): \+?[\d\s().-]{7,20} against Call +1 (555) 012-3456 now matches +1 (555) 012-3456. It allows an optional +, then 7–20 characters of digits, spaces, parens, dots, and hyphens. Strict phone validation by country is better done with a dedicated library — this pattern is for finding candidate numbers in text.
URL: https?:\/\/[^\s/$.?#].[^\s]* against Visit https://www.toolsofweb.com/tools today matches the full URL. It finds http with optional s, the literal ://, then a run of non-space characters.
Date (DD/MM/YYYY): \d2\/\d2\/\d4 against Meeting on 05/10/2026 matches 05/10/2026. Note this validates the shape, not the calendar — it would also match 99/99/9999. Combine it with group capture (\d2)\/(\d2)\/(\d4) and check the numeric ranges in code for real validation.
Hex color: #[0-9a-fA-F]6\b against Brand blue is #1d4ed8 here matches #1d4ed8. Change {6} to {3} for shorthand colors like #fff.
Flags, explained with examples
Flags change what “match” means. /cat/g finds every cat; without g it stops at the first. /cat/i also matches CAT and Cat — try pasting mixed-case text with i off and on to feel the difference. /^\d+$/m matches lines that are entirely digits inside multi-line text — without m, ^ and $ only match the very start and end of the whole string. s is the fix when .* mysteriously stops at line breaks: it lets . match newlines too. u matters for emoji and non-Latin scripts (without it, some characters are treated as two broken halves); y (sticky) is a niche tool for parsers that match position by position.
Flags can be combined: /pattern/gi finds every case-insensitive match. A classic beginner trap is testing with i on and deploying without it — the tester’s flag toggles exist precisely so you can confirm which flags your final pattern needs.
Greedy vs lazy quantifiers
Quantifiers like * and + are greedy by default: they match as much text as possible. Try <.*> against <b>hi</b> — it matches the entire string, because .* stretches from the first < all the way to the last >. Adding ? makes a quantifier lazy: it matches as little as possible. <.*?> matches <b> and </b> separately.
This explains a whole class of “why did it match too much?” bugs. Whenever your pattern spans from one delimiter to a later one (quotes, tags, brackets), ask yourself whether you want greedy (everything up to the last delimiter) or lazy (up to the next delimiter). An alternative that avoids the question entirely is a negated character class: <[^>]*> matches “a <, then anything that isn’t >, then a >” — unambiguous in both modes.
Debugging: when your regex matches nothing
Work through this checklist in order: 1. Is the pattern itself valid? An unclosed [ or ( is the #1 cause — the tester shows the exact error. 2. Did you forget the g flag and only see the first match? 3. Are ^/$ anchors too strict? Try removing them. 4. Is case the issue? Try i. 5. Test against the simplest input that should match, then grow the test text until it breaks — the break point is your bug.
Two more suspects worth checking: whitespace differences (tabs and non-breaking spaces are invisible in test text — \s handles them all), and greedy matching (your pattern matched, but it matched too much, so the group you expected is empty). For the second case, check the match list positions: if the match spans further than expected, switch a * to a lazy *? or a negated class.
Capture groups and replace, in practice
Groups are regex’s real power tool. To reformat 2026-10-05 into 05/10/2026, match (\d4)-(\d2)-(\d2) and replace with $3/$2/$1. To swap emails around, (\w+)@(\w+) → $2@$1 turns hello@toolsofweb.com into toolsofweb.com@hello — a trick sometimes used to obscure addresses.
Preview every replacement in the tester first: back-reference mistakes are silent, and seeing the result before it touches your code saves real debugging time. A common slip is using $10 when you mean $1 followed by a literal 0 — the tester shows you the wrong output immediately instead of letting it reach production.
When regex is (and isn’t) the right tool
Regex is the right choice when you’re matching shapes of text: emails, dates, numbers, code tokens, log lines. It’s the wrong choice when the text has nesting or structure: parsing HTML/XML, full email addresses per RFC, or balanced brackets. Those need a real parser. A practical middle ground: use regex to find candidates and code to validate them — for example, extract date-shaped strings with \d2\/\d2\/\d4 and check the calendar math in code. And if you only need “does the text contain X?”, a plain string search is faster and more readable than regex.
Helpful follow-up tools
- Normalize matched text with the Text Cleaner.
- Compare pattern versions with the Text Diff Checker.
- Validate the JSON your regex extracts with the JSON Formatter.
FAQs
Paste your pattern into a regex tester, type or paste test text, and every match highlights instantly. ToolsOfWeb’s free tester shows matches with capture groups, flag toggles, and a replace preview — all in your browser.
g finds all matches instead of the first; i ignores case; m makes ^ and $ match line boundaries; s lets . match newlines; u enables Unicode mode; y (sticky) matches only from the current position. Toggle them in a tester to see the difference live.
Check the three usual suspects: an unclosed bracket or parenthesis (the tester shows the exact error), a missing flag (often g), and anchors — ^ and $ require the whole string to match. Test against the simplest possible input first, then add complexity.
Parentheses create groups numbered left to right: (\w+)@(\w+) captures the username as $1 and the domain as $2. In a replacement string, $1 and $2 reinsert what each group matched — perfect for reformatting text.
Quantifiers like * and + are greedy by default: they match as much text as possible. Adding ? makes them lazy — they match as little as possible. For example, <.*> on "<b>hi</b>" matches the whole string, while <.*?> matches "<b>" and "</b>" separately.
With a client-side tester, no. ToolsOfWeb’s regex tester runs JavaScript’s built-in RegExp engine in your browser — patterns and test text never leave your device.
Test your regex now
Use Regex Tester — free, private, and instant. Live highlighting, capture groups, flags, and replace preview.
