What is a regex tester?
A regex tester is a tool that lets you write a regular expression (regex) and instantly see what it matches in a piece of sample text. Instead of running your code again and again to check a pattern, you can watch matches highlight live, inspect capture groups and try replacements before you put the pattern into your project.
This free online regex tester uses the JavaScript (ECMAScript) regular expression engine in your browser. It highlights every match, lists the position and groups of each match, previews search and replace results, and generates ready-to-use code for JavaScript, Python and PHP. Your pattern and text are processed locally and are never uploaded.
Regex syntax cheat sheet
These are the building blocks you will use in almost every pattern. Click any token in the Quick reference panel above to insert it into your expression.
| Token | Meaning | Example |
|---|---|---|
. | Any character except a line break (use the s flag to include line breaks) | a.c matches "abc", "a-c" |
\d \D | A digit / a non-digit | \d{3} matches "123" |
\w \W | A word character (letter, digit, underscore) / anything else | \w+ matches "user_1" |
\s \S | Whitespace (space, tab, newline) / non-whitespace | \s+ matches runs of spaces |
\b \B | A word boundary / not a word boundary | \bcat\b matches "cat" but not "concat" |
^ $ | Start / end of the text (or of each line with the m flag) | ^\d+$ matches digits only |
* + ? | 0 or more / 1 or more / 0 or 1 of the previous item | colou?r matches "color" and "colour" |
{n} {n,m} | Exactly n / between n and m repeats | \d{2,4} matches 2 to 4 digits |
[abc] [^abc] | Any one of the listed characters / any character not listed | [A-Za-z] matches one letter |
(…) | A capture group, available as $1, $2 and so on | (\d+)-(\d+) |
(?:…) | A non-capturing group, used only for grouping | (?:ab)+ |
(?<name>…) | A named capture group, available as $<name> | (?<year>\d{4}) |
a|b | Alternation: match a or b | cat|dog |
\1 | A backreference to the first capture group | (\w)\1 matches "ll" in "hello" |
(?=…) (?!…) | Lookahead: followed by / not followed by | \d+(?= USD) |
(?<=…) (?<!…) | Lookbehind: preceded by / not preceded by | (?<=\$)\d+ |
To match a character that has a special meaning, put a backslash in front of it. For example, \. matches a literal dot and \( matches a literal opening parenthesis.
Common regex examples you can copy
Use these patterns as a starting point. Open the Examples panel in the tester to load a version with sample text, then adjust it for your own data.
| Use case | Pattern | Matches |
|---|---|---|
| Email address | [\w.+-]+@[\w-]+(?:\.[\w-]+)+ | hello@example.com |
| URL | https?:\/\/[^\s/$.?#].[^\s]* | https://toolforge.com/regex-tester |
| Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} | 2026-11-19 |
| Time (24 hour) | ([01]\d|2[0-3]):[0-5]\d | 09:30, 23:59 |
| IPv4 address (loose) | \b(?:\d{1,3}\.){3}\d{1,3}\b | 192.168.1.1 |
| Hex color | #(?:[0-9a-fA-F]{3}){1,2}\b | #fff, #4f46e5 |
| Digits only | ^\d+$ | 20261119 |
| Trim leading and trailing spaces | ^\s+|\s+$ | " text " |
| Repeated words | \b(\w+)\s+\1\b | "the the" |
| HTML tag | <\/?[a-z][^>]*> | <div class="a"> |
| Password with upper, lower and digit (8+ chars) | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ | Passw0rd |
| Hashtag | #\w+ | #regex |
Regex flags explained
Flags change how the whole pattern behaves. You can toggle them with the buttons under the pattern box.
| Flag | Name | What it does |
|---|---|---|
g | Global | Finds all matches instead of stopping after the first one. |
i | Ignore case | Treats uppercase and lowercase letters as the same. |
m | Multiline | Makes ^ and $ match at the start and end of every line. |
s | Dot all | Lets . match line breaks too. |
u | Unicode | Enables full Unicode support, including characters outside the basic plane and \p{…} properties. |
y | Sticky | Matches only at the exact position where the last match ended. |
Greedy vs lazy matching and backtracking
By default, quantifiers such as *, + and {n,m} are greedy: they match as much text as they can. Adding a ? after the quantifier makes it lazy, so it matches as little as possible.
For example, on the text <b>one</b> and <b>two</b>, the greedy pattern <b>.*</b> returns one long match from the first tag to the last, while the lazy pattern <b>.*?</b> returns two separate matches.
What is catastrophic backtracking?
When a pattern fails, the engine goes back and tries other ways to match. With nested or overlapping quantifiers, such as (a+)+$, the number of attempts can grow exponentially, and a short input can freeze a program. To avoid this, remove nested quantifiers, make alternatives mutually exclusive, use specific character classes instead of .*, and anchor your pattern. This tester runs matching in a background worker and stops it after 1.5 seconds, so you can experiment safely.
Common regex mistakes and how to fix them
- Forgetting to escape special characters. Characters like
. * + ? ( ) [ ] { } ^ $ | \have special meanings. Escape them with a backslash to match them literally. - Using
.*everywhere. It is greedy and often matches too much. Prefer a lazy quantifier or a negated class such as[^"]*. - Missing the global flag. Without
g, only the first match is returned. - Forgetting anchors for validation. To check that the whole string matches, use
^and$. Without them, a partial match is enough. - Using
^and$on multi-line text. Turn on themflag if you want them to work per line. - Expecting the same behavior in every language. Engines differ. For example, named groups are written
(?<name>…)in JavaScript and(?P<name>…)in Python.
Using your regex in JavaScript, Python and PHP
The Code tab generates a snippet for your current pattern, flags and text. Here is the basic idea in each language, using a date pattern.
JavaScript
const re = /(\d{4})-(\d{2})-(\d{2})/g;
const text = "Released 2026-11-19";
for (const m of text.matchAll(re)) {
console.log(m[0], m[1], m[2], m[3]);
}
Python
import re
text = "Released 2026-11-19"
for m in re.finditer(r"(\d{4})-(\d{2})-(\d{2})", text):
print(m.group(0), m.groups())
PHP
$text = "Released 2026-11-19";
preg_match_all('/(\d{4})-(\d{2})-(\d{2})/', $text, $matches, PREG_SET_ORDER);
print_r($matches);
What is regex used for?
- Form validation: check emails, phone numbers, postal codes and usernames before submitting.
- Search and replace: rename files, reformat dates or clean up text in editors and scripts.
- Data extraction: pull IDs, prices, links or hashtags out of logs, web pages and documents.
- Data cleaning: remove extra spaces, strip HTML tags or normalize separators.
- URL routing and rewrites: match paths in web servers and frameworks.
- Log analysis: filter errors and timestamps from large log files.
Regex is powerful, but it is not the best tool for everything. For nested structures such as full HTML or JSON, use a proper parser. Looking for other utilities? Browse all developer tools, or try our image compressor and JPG to PDF converter.