How to Test Regular Expressions Online (Step-by-Step Guide)
This guide has a free tool → Open ToolBox Regex Tester
# How to Test Regular Expressions Online (Step-by-Step Guide)
Regular expressions are one of the most powerful tools in a developer's arsenal and one of the most frustrating to write. A misplaced character changes everything. An untested pattern fails silently on edge cases you did not consider. Testing regex in your production code means a slow feedback loop of write, run, check, fix, repeat.
This guide explains how to test regular expressions online for instant visual feedback, walks through the most common patterns with examples, and covers everything you need to write regex that actually works in production.
---
Why Test Regex in a Browser Tool?
The Problem With Testing Regex in Your Code
When you test regex inside your application code, the feedback loop looks like this:
- Write or edit your pattern
- Save the file
- Run the application or test suite
- Check the output
- Go back to step 1
Even with hot reloading, this is slow. A browser-based regex tester collapses this loop to zero. You type a character in the pattern field and see matches update in real time.
Visual Feedback Changes How You Think
Seeing matches highlighted in your test text does something that reading console.log output does not. You can immediately see:
- Which parts of the text match
- Whether matches overlap or miss expected content
- How capture groups break down the match
- Whether the pattern is too greedy or too restrictive
Visual feedback makes regex debugging intuitive rather than intellectual.
Your Test Data Stays Private
Many developers test regex against real-world data: log files, database exports, user-generated content. A client-side regex tester keeps that data in your browser. Nothing is sent to a server.
---
Regex Tester
Free online regex tester - test and debug regular expressions with live matching and highlights
JWT Decoder
Free online JWT decoder - decode and inspect JSON Web Tokens without sending them to a server
Regex Playground
Free online regex playground - build, test, and debug regular expressions with real-time match highlighting
How to Test Regex With ToolBox
Step 1: Open the Regex Tester
Go to ToolBox Regex Tester. The interface has three main sections:
- Pattern input at the top, with flag toggles
- Test text area below the pattern input
- Match results panel showing matched text, match indices, and capture groups
Step 2: Enter Your Pattern
Type your regex pattern in the pattern field. Do not include the surrounding slashes (/pattern/flags) - just the pattern itself. The flags are separate controls.
For example, to match email addresses:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Step 3: Set Your Flags
Click the flag toggle buttons to enable the options you need:
| Flag | Short | What It Does |
|---|---|---|
| global | g | Find all matches, not just the first one |
| case insensitive | i | Treat uppercase and lowercase as equivalent |
| multiline | m | Make ^ and $ match start/end of each line |
| dotAll | s | Make . match newline characters (\n) |
| unicode | u | Enable full Unicode matching |
| sticky | y | Match only from the position where the last match ended |
For most use cases, you want g (global) enabled so you see all matches, not just the first.
Step 4: Paste Your Test Text
Paste the text you want to match against in the text area. Use realistic examples, not just one line. The more varied your test text, the better you will understand how your pattern behaves.
Matches appear highlighted in real time. The panel below shows:
- The matched text for each match
- The start and end index of each match
- The content of each capture group
Step 5: Refine Your Pattern
Adjust the pattern and watch matches update instantly. This is the core loop: see what matches, see what does not, adjust, repeat.
---
Understanding Regex Syntax
Before looking at specific patterns, you need a solid grasp of the building blocks.
Literal Characters
Most characters match themselves. The pattern cat matches the text "cat". Simple.
Metacharacters
These characters have special meaning and need escaping with \ when you want to match them literally:
. * + ? ^ $ { } [ ] ( ) | \To match a literal period, write \.. To match a literal parenthesis, write \(.
Character Classes
Square brackets define a set of characters where any one character in the set can match:
[abc] - matches "a", "b", or "c"
[a-z] - matches any lowercase letter
[A-Z] - matches any uppercase letter
[0-9] - matches any digit
[a-zA-Z] - matches any letter
[^abc] - matches any character that is NOT "a", "b", or "c"Shorthand Character Classes
These are commonly used character class shortcuts:
| Shorthand | Equivalent | Matches |
|---|---|---|
\d | [0-9] | Any digit |
\D | [^0-9] | Any non-digit |
\w | [a-zA-Z0-9_] | Any word character |
\W | [^a-zA-Z0-9_] | Any non-word character |
\s | [ \t\r\n\f\v] | Any whitespace |
\S | [^ \t\r\n\f\v] | Any non-whitespace |
. | [^\n] | Any character except newline |
Quantifiers
Quantifiers control how many times a pattern repeats:
| Quantifier | Meaning | Example |
|---|---|---|
* | 0 or more | a* matches "", "a", "aa", "aaa" |
+ | 1 or more | a+ matches "a", "aa", "aaa" but not "" |
? | 0 or 1 | a? matches "" or "a" |
{n} | Exactly n times | a{3} matches "aaa" only |
{n,} | At least n times | a{2,} matches "aa", "aaa", "aaaa" |
{n,m} | Between n and m times | a{2,4} matches "aa", "aaa", "aaaa" |
Anchors
Anchors match positions in the text, not characters:
| Anchor | Meaning |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
Word boundaries (\b) are useful when you want to match a whole word but not part of a longer word. The pattern \bcat\b matches "cat" in "the cat sat" but not in "catalog".
Alternation
The pipe character | means "or":
cat|dog - matches "cat" or "dog"Groups
Parentheses create groups:
// Capturing group - captures the match
(abc)
// Non-capturing group - groups without capturing
(?:abc)
// Named capturing group
(?<name>abc)---
Common Regex Patterns With Explanations
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Breaking this down:
^- start of string[a-zA-Z0-9._%+-]+- one or more characters valid in an email local part@- literal @ symbol[a-zA-Z0-9.-]+- one or more characters valid in a domain name\.- literal period[a-zA-Z]{2,}- top-level domain, at least 2 letters$- end of string
Important caveat: email validation regex cannot fully validate an email address according to RFC 5322. Use it for basic format checking, not as a substitute for sending a confirmation email.
URL Matching
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)Breaking this down:
https?- "http" or "https":\/\/- literal "://"(www\.)?- optional "www."[-a-zA-Z0-9@:%._+~#=]{1,256}- domain characters\.[a-zA-Z0-9()]{1,6}- top-level domain\b- word boundary([-a-zA-Z0-9()@:%_+.~#?&//=]*)- optional path, query, fragment
Phone Numbers (US Format)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}This matches:
(555) 123-4567555-123-4567555.123.45675551234567
Phone Numbers (International E.164 Format)
\+[1-9]\d{1,14}E.164 format: starts with + followed by country code and number, 2-15 digits total.
IPv4 Address
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\bThis properly validates that each octet is 0-255, not just any number.
The simpler version \b(?:\d{1,3}\.){3}\d{1,3}\b matches the pattern but allows invalid octets like 999.
IPv6 Address
([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}This matches the full 8-group format. A complete IPv6 regex that handles all abbreviation forms is much longer and usually not worth writing by hand.
Date Formats
ISO 8601 (YYYY-MM-DD):
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])US format (MM/DD/YYYY):
(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}Time (24-Hour Format)
^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$Matches 13:45, 09:00, 23:59:59.
Time (12-Hour Format)
^(1[0-2]|0?[1-9]):([0-5][0-9])\s?([AaPp][Mm])$Matches 3:45 PM, 12:00 AM, 09:30pm.
Credit Card Numbers
Visa:
^4[0-9]{12}(?:[0-9]{3})?$Mastercard:
^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$Hex Color Codes
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Matches #FF5733 and shorthand #F53.
RGB Color
^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$UUID (Version 4)
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$The 4 in the third group and [89ab] in the fourth group are version 4 specific.
HTML Tags
<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>Note: Parsing HTML with regex is famously unreliable for anything beyond simple cases. Use an HTML parser for production code.
JWT Token
^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$Matches the three-part Base64URL format of a JWT. Use the ToolBox JWT Decoder to actually decode and inspect a JWT.
Semantic Version
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$Matches 1.0.0, 2.3.4-beta.1, 1.0.0+build.123.
Slug
^[a-z0-9]+(?:-[a-z0-9]+)*$Matches URL slugs like my-blog-post-title.
Username
^[a-zA-Z0-9_-]{3,16}$Matches usernames that are 3-16 characters long, containing letters, numbers, underscores, and hyphens.
Password Strength
At least 8 characters, at least one uppercase, one lowercase, one digit, one special character:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$This uses lookahead assertions ((?=...)) to require each character type without constraining their order.
Postal Codes
US ZIP Code:
^\d{5}(?:-\d{4})?$UK Postcode:
^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$Canadian Postal Code:
^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] ?\d[ABCEGHJ-NPRSTV-Z]\d$File Extensions
\.(jpg|jpeg|png|gif|webp|svg)$Match case-insensitively with the i flag.
---
Greedy vs. Lazy Quantifiers
This is one of the most common sources of regex bugs. Understanding the difference prevents hours of debugging.
Greedy Quantifiers
By default, quantifiers are greedy. They match as much as possible while still allowing the overall pattern to match.
// Test text: <div>first</div><div>second</div>
// Greedy pattern: <div>.*</div>
// Matches: <div>first</div><div>second</div> (entire string!)The greedy .* consumed everything from the first <div> to the last </div>.
Lazy Quantifiers
Add ? after a quantifier to make it lazy. Lazy quantifiers match as little as possible.
// Test text: <div>first</div><div>second</div>
// Lazy pattern: <div>.*?</div>
// Matches: <div>first</div> and <div>second</div> (two separate matches)The lazy .*? stopped at the first </div> it encountered.
When to Use Each
Use greedy when you want to match to the end of a structure. Use lazy when you want to match the shortest possible string between two delimiters.
// Greedy - match everything inside outermost quotes
".*"
// Lazy - match each quoted string separately
".*?"---
Capturing Groups vs. Non-Capturing Groups
Capturing Groups
Parentheses create a capturing group. The content of each group is available separately after a match.
// Pattern: (\d{4})-(\d{2})-(\d{2})
// Text: 2024-03-15
// Group 1: 2024
// Group 2: 03
// Group 3: 15
const match = '2024-03-15'.match(/(\d{4})-(\d{2})-(\d{2})/);
const [fullMatch, year, month, day] = match;Non-Capturing Groups
Use (?:...) when you need grouping for alternation or quantifiers but do not need to access the group's content separately. This is cleaner and slightly more efficient.
// Capturing - creates a capture group you do not need
(https?|ftp)://
// Non-capturing - groups the alternation without capturing
(?:https?|ftp)://Named Capture Groups
Named groups ((?<name>...)) make patterns more readable and make extracting values by name possible:
// Pattern with named groups
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
// In JavaScript
const match = '2024-03-15'.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
const { year, month, day } = match.groups;---
Lookahead and Lookbehind
Lookaheads and lookbehinds match positions based on what comes before or after, without including that content in the match.
Positive Lookahead (?=...)
Match a position followed by a specific pattern:
// Match "cat" only when followed by "s"
cat(?=s)
// Matches: "cats" (matches "cat", not "cats")
// Does not match: "cat" aloneNegative Lookahead (?!...)
Match a position NOT followed by a specific pattern:
// Match "cat" only when NOT followed by "s"
cat(?!s)
// Matches: "cat" alone
// Does not match: "cats"Positive Lookbehind (?<=...)
Match a position preceded by a specific pattern:
// Match digits preceded by a dollar sign
(?<=\$)\d+
// In "$100", matches "100" without the "$"Negative Lookbehind (?<!...)
Match a position NOT preceded by a specific pattern:
// Match digits NOT preceded by a dollar sign
(?<!\$)\d+Lookaheads and lookbehinds are powerful for extracting values from formatted text without capturing delimiters.
---
Testing Regex in Different Languages
The ToolBox regex tester uses JavaScript's RegExp engine. JavaScript regex syntax is compatible with most modern languages, but there are differences to know.
JavaScript vs. Other Languages
| Feature | JavaScript | Python | Java | PHP |
|---|---|---|---|---|
| Named groups | (?<name>...) | (?P<name>...) | (?<name>...) | (?P<name>...) |
| Lookbehind | Limited length | Variable length | Variable length | Variable length |
| Unicode | With u flag | Built-in | Built-in | With u flag |
| Inline flags | No | (?i) inside | Yes | (?i) inside |
\d matches | ASCII digits | ASCII or Unicode | Unicode | ASCII or Unicode |
Python-Specific Notes
Python uses re module by default and regex module for extended features. Named groups use (?P<name>...) syntax. re.IGNORECASE, re.MULTILINE, re.DOTALL are flags passed to compile functions.
import re
pattern = re.compile(r'(\d{4})-(\d{2})-(\d{2})', re.MULTILINE)
matches = pattern.findall(text)JavaScript Implementation
// Creating a regex with the RegExp constructor (useful for dynamic patterns)
const pattern = new RegExp('[a-z]+', 'gi');
// Using regex literal
const pattern = /[a-z]+/gi;
// Test if pattern matches
pattern.test(text);
// Find all matches
text.match(pattern);
// Replace matches
text.replace(pattern, replacement);
// Find match with index
pattern.exec(text);Common Cross-Language Patterns That Work Everywhere
When you need a pattern that works across JavaScript, Python, PHP, and Java, stick to:
- Basic character classes:
[a-z],[0-9] \d,\w,\s(behavior may differ slightly for Unicode)*,+,?,{n,m}quantifiers^,$anchors with appropriate flags(...)capturing groups(?:...)non-capturing groups
Avoid language-specific extensions when writing cross-language patterns.
---
Testing Edge Cases
A pattern that works on your test data might fail on real-world input. Always test these categories:
Empty Input
What happens when the input is an empty string? Your pattern should either match nothing or specifically handle empty input.
// Pattern: [a-z]+
// Empty string: no match (correct)
// Pattern: [a-z]*
// Empty string: matches "" (might not be what you want)Very Long Input
Some regex patterns are vulnerable to catastrophic backtracking on long inputs. If your pattern has nested quantifiers ((a+)+), test with long strings to verify performance.
Special Characters
Test with input containing characters that could be interpreted as regex metacharacters: ., *, +, ?, (, ), [, ], {, }, |, \, ^, $.
Unicode Characters
Test with non-ASCII characters, especially if your pattern uses . or \w and might receive international input.
Boundary Cases
For patterns that match specific lengths, test the boundaries:
- Exactly the minimum length
- Exactly the maximum length
- One below the minimum
- One above the maximum
---
Regex Tester vs. Regex Playground
ToolBox offers two regex tools for different use cases:
Regex Tester
The Regex Tester is focused and fast:
- Real-time match highlighting
- Match count and index display
- Capture group details
- Flag toggles
- Best for: quick testing, debugging a single pattern
Regex Playground
The Regex Playground is full-featured:
- Everything the Tester has
- Find-and-replace mode with preview
- Library of 35+ common patterns
- URL sharing (send your pattern and test text to someone else)
- Better for complex patterns with multiple groups
Start with the Tester for quick validation. Move to the Playground when you need to build or document a more complex pattern.
---
Practical Workflow: Building a Pattern Step by Step
Let us build a pattern for matching a log line format step by step. The format is:
[2024-03-15 14:32:11] ERROR app.js:47 Connection timeout after 30sStep 1: Match the Timestamp
\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]Test: [2024-03-15 14:32:11] - matches.
Step 2: Match the Log Level
\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] (ERROR|WARN|INFO|DEBUG)Test: adds capture for ERROR.
Step 3: Match the File and Line
\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] (ERROR|WARN|INFO|DEBUG) ([\w.]+):(\d+)Now captures app.js and 47 separately.
Step 4: Match the Message
\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] (ERROR|WARN|INFO|DEBUG) ([\w.]+):(\d+) (.+)The final group captures the rest of the line.
Step 5: Add Named Groups for Clarity
\[(?<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\] (?<level>ERROR|WARN|INFO|DEBUG) (?<file>[\w.]+):(?<line>\d+) (?<message>.+)Now you can extract values by name: match.groups.timestamp, match.groups.level, etc.
This step-by-step approach is the right way to build complex patterns. Start simple, add one piece at a time, and verify at each step.
---
Why Browser-Based Regex Testing Is Faster
| Approach | Time per iteration | Feedback type |
|---|---|---|
| Inline in application code | 5-30 seconds | Console output |
| Unit test | 2-10 seconds | Test pass/fail |
| Browser regex tester | Instant | Visual highlighting |
The browser approach is 10-100x faster per iteration. For a pattern that takes 20 iterations to get right, that is the difference between 10 minutes and 30 seconds.
This is not an exaggeration. The visual feedback loop changes how you think about and debug patterns.
---
Summary
A browser-based regex tester eliminates the slow feedback loop that makes regex development frustrating. Instead of writing code, running it, and reading output, you type a pattern and see matches highlighted instantly.
The key skills for effective regex work are:
- Understanding character classes, quantifiers, and anchors
- Knowing when to use greedy vs. lazy matching
- Using capture groups to extract structured data
- Always testing edge cases including empty input, special characters, and boundary lengths
For quick pattern testing and debugging, use the ToolBox Regex Tester. For building complex patterns with a library of starting points and find-and-replace preview, use the Regex Playground.
Both run entirely in your browser. Your test data never leaves your device.
---
Try It Free
Open ToolBox Regex Tester - free, private, instant match highlighting. Test any pattern against real text and see results as you type.
Want more power? The Regex Playground adds a 35+ pattern library, find-and-replace mode, and shareable URLs.
You might also like
Want higher limits, batch processing, and AI tools?