Step-by-step guide: Regex Basics for Beginners: A Practical Guide
How to use the Regex Tester
- Type your pattern between the slashes. Do not include the slashes themselves.
- Tick the flags you need. Without
g, only the first match is found. - Paste test text containing examples that should and should not match. Matches are highlighted as you type.
- Check the table for each match’s position and the contents of every capture group.
- Enter a replacement to preview the result of
String.replace, using$1,$<name>or$&.
Example: extracting parts of email addresses
The default pattern uses two named groups:
(?<user>[\w.+-]+)@(?<domain>[\w-]+\.[\w.]+)
In the sample text it finds ada@example.com and grace.hopper@navy.example.org, reporting user and domain separately for each. It correctly ignores someone@ and @nowhere.com. The replacement $<user> [at] $<domain> turns the addresses into a lightly obfuscated form.
This pattern is deliberately simple. Fully validating email addresses with a regex is notoriously difficult; for forms, use <input type="email"> and a confirmation email.
Quick reference
| Syntax | Meaning |
|---|---|
. | Any character except a line break (any at all with the s flag) |
\d \w \s | Digit, word character (letter, digit, underscore), whitespace |
\D \W \S | The opposite of each of the above |
[abc] [^abc] [a-z] | Any of, none of, a range |
^ $ | Start and end of the text (of each line with m) |
\b | Word boundary |
* + ? {2,5} | 0 or more, 1 or more, optional, between 2 and 5 |
*? +? | Lazy versions: match as little as possible |
(…) (?:…) (?<name>…) | Capturing, non-capturing and named groups |
a|b | Either a or b |
(?=…) (?!…) (?<=…) (?<!…) | Lookahead, negative lookahead, lookbehind, negative lookbehind |
\p{L} | Any letter in any language (requires the u flag) |
JavaScript regular expressions
The tester uses your browser’s JavaScript regular-expression engine (ECMAScript), so results match what RegExp, String.match and String.replace do in browsers and Node.js. Other languages differ in places: Python uses (?P<name>…) for named groups, PCRE supports possessive quantifiers and recursion, and .NET has different lookbehind rules. Test patterns in the environment where they will run.
Why patterns run in a worker
Certain patterns, such as (a+)+$, can take exponential time on some inputs — a problem called catastrophic backtracking that has caused real outages. The tester runs each pattern in a separate background worker with a time limit. If a pattern takes too long, the worker is stopped and you are told why, instead of the browser tab freezing.
Limitations to keep in mind
- Only JavaScript (ECMAScript) syntax is supported.
- At most 5,000 matches are listed and highlighted, to keep the page responsive.
- Patterns are stopped after 2 seconds of processing.
How your data is handled
Patterns and test text stay in your browser. They are compiled with the RegExp constructor — never executed as code — and are not sent anywhere.
Frequently asked questions
Why does my pattern only find one match?
Tick the g (global) flag. Without it, regular expressions stop after the first match.
Do I need to escape forward slashes?
No. The pattern box takes the pattern on its own, so / is an ordinary character. You only need \/ when writing a regex literal in JavaScript source code.
How do I match a literal dot or question mark?
Escape it with a backslash: \. or \?. Inside square brackets, most special characters are literal, so [.?] also works.
What does “Invalid regular expression” mean?
The pattern has a syntax error, such as an unclosed group or bracket, or a quantifier with nothing before it. The message from the browser explains which.