Regex Tester and Validator

Test JavaScript regular expressions instantly with highlighted matches, capture groups, a replace preview and a pattern explainer.

Free · Instant · Private
Quick answer: Type a pattern such as \b\w+@\w+\.\w{2,}\b and some test text, then enable g to see every match. A regular expression is evaluated left to right; each match records its start index, end index and any capture groups. This page uses the ECMAScript engine inside your own browser, so the result is exactly what JavaScript would return.
Match summary
3 matches

Highlighted test string

Every match is wrapped in a marker. HTML in your text is escaped before display.

Match table with capture groups

Indices are zero based, measured in UTF-16 code units, exactly as returned by JavaScript RegExp.exec().

#StartEndMatchCapture groups
Replacements made
3

Token by token explanation

A readable breakdown of the pattern you typed. Anchors, classes, quantifiers and groups are described separately.

PositionTokenMeaning

Ready to paste

Regex syntax cheat sheet

The most useful ECMAScript tokens. Every row is plain text, so you can copy it into your code.

TokenMeaningExample

What Is a Regular Expression?

A regular expression, usually shortened to regex, is a compact description of a set of strings. Instead of writing a loop that checks every character by hand, you describe the shape of the text you want: a digit, a word, a repeated group, a position at the start of a line. A regex engine then walks the input and reports the substrings that fit.

Regexes are used for four jobs. Searching finds text inside a larger document. Validation checks that a whole input has the expected shape. Extraction pulls fields out of structured text using capture groups. Substitution replaces matches, optionally reusing captured pieces. This tool supports all four, entirely inside your browser.

How the Tester Works

There is no hidden formula to memorise; a regex engine performs a left to right scan. The important rules are these:

  1. The engine starts at the beginning of the input and tries the pattern at each position.
  2. At the first position where the pattern succeeds, it records the matched substring and its zero based start and end indices.
  3. If the g flag is set, the next search begins at the end of that match; otherwise the scan stops after one match.
  4. Greedy quantifiers such as *, + and {n,m} consume as much as possible while still allowing the rest of the pattern to match. Adding ? makes them lazy, so they consume as little as possible.
  5. If a match has zero width, the engine advances one character (or one Unicode code point in u mode) before trying again. Without that step, patterns like ^ or a* would loop forever.

Formally, for a pattern P and input S, the result is the ordered list of non-overlapping substrings S[i..j) where P matches at i and j is the end index. Capture group k is the substring covered by the k-th pair of parentheses, or undefined when that group did not participate.

Flags and What They Change

FlagNameEffect
gglobalReturn all matches instead of only the first, and replace all occurrences.
iignoreCaseMatch letters without regard to case.
mmultilineMake ^ and $ match at line boundaries instead of only string boundaries.
sdotAllLet . also match newline characters.
uunicodeTreat the pattern as Unicode code points and enable escapes such as \p{Letter}.

Only these five flags are exposed because they are the ones supported by ECMAScript RegExp. Other engines add flags such as x for verbose mode; JavaScript does not, so copying a pattern from a PCRE or Python context can silently change its meaning.

Capturing Groups and Named Groups

Parentheses do two things: they group tokens so a quantifier applies to all of them, and they capture the matched text. (?:...) groups without capturing. (?<year>\d{4}) gives a group a name. Inside a replacement string, $1 refers to the first group, $<year> to a named group, $& to the whole match, and $$ to a literal dollar sign.

The Match Algorithm and Its Limits

This page caps the test string at 20,000 characters and the match list at 500 rows so that a pathological pattern cannot flood the interface. ECMAScript regexes use backtracking, so a pattern with nested quantifiers such as (a+)+$ can be slow on long input. If you need guaranteed linear time matching in production, use a non-backtracking engine; the references below name the specifications involved.

Worked Examples

GoalPatternFlagsTest textResult
Find email like strings(\w+)@(\w+)\.(\w{2,})g[email protected] and [email protected]2 matches; groups ada / example / com and grace / work / org
Find a phone style number\d{3}-\d{4}gCall 555-1234 or 555-9876555-1234, 555-9876
Match an ISO date\d{4}-\d{2}-\d{2}gDue 2026-12-04, not 04/12/20262026-12-04
Match hex colours#[0-9a-fA-F]{6}g#7F56D9 and #fff#7F56D9 only
Find a duplicated word\b(\w+)\s+\1\bgithe the catthe the, group 1 = the
Match whole lines^TODO.*$gmTODO write tests
DONE ship
TODO write tests
Zero width boundaries\bgone two4 matches at positions 0, 3, 4, 7
Swap two fields(\w+),(\w+)gDoe,JaneReplace with $2 $1 gives Jane Doe

Try any of these with the preset buttons above. Notice that the ISO date pattern matches the shape but does not prove the date exists; 2026-99-99 also matches. Shape checking and value checking are different jobs.

Regex Syntax Cheat Sheet

CategoryTokensWhat they mean
Character classes. \d \w \s [abc] [^abc]Any character, a digit, a word character, whitespace, one of a set, or anything not in a set.
Anchors^ $ \b \BStart, end, word boundary and non-boundary positions. Anchors consume no characters.
Quantifiers* + ? {n} {n,} {n,m}Zero or more, one or more, optional, exactly n, at least n, between n and m.
Lazy forms*? +? ?? {n,m}?The same quantifiers, but taking the shortest match that still works.
Groups(...) (?:...) (?<name>...) \1Capture, group without capturing, named capture and a backreference.
Lookaround(?=...) (?!...) (?<=...) (?<!...)Require or forbid following or preceding text without consuming it.
Alternationa|bMatch either the left or the right alternative.
Escapes\. \\ \n \t \uFFFFA literal dot, a backslash, newline, tab and a Unicode escape.

Common Mistakes

  • Forgetting the g flag. Without it you get one match and a replacement affects only the first occurrence.
  • Using . to match across lines. By default the dot stops at a newline; use the s flag or [\s\S].
  • Forgetting to escape a literal dot. example.com also matches exampleXcom; write example\.com.
  • Overly greedy patterns. <.*> swallows everything between the first and last angle bracket. Use <.*?> or a negated class.
  • Assuming a shape check validates a value. \d{2} happily matches 99 as a month. Check ranges in code.
  • Copying patterns between engines. PCRE, Python, RE2 and ECMAScript differ in lookbehind, Unicode properties and flags.
  • Anchoring incorrectly. Use ^...$ with m for line based checks, and be aware that $ can match before a final newline.
  • Ignoring zero width matches. A loop over exec() that never advances lastIndex will hang on ^ or \b.

Practical Uses and Long-Tail Searches

People commonly look for a "regex tester with match highlighting", a "JavaScript regex validator with flags", a "regex replace preview with capture groups", a "named capture group tester", or a "regex cheat sheet with examples". Typical jobs include cleaning log lines, extracting IDs from URLs, reformatting CSV like text, checking form fields, and preparing a pattern before pasting it into a Node.js script. When the extracted data is structured, the JSON Formatter and Validator is a natural next step.

Remember that a regex is a text tool, not a parser. Do not use one to parse HTML, nested JSON or balanced brackets; use a real parser for those. Patterns for scheduled jobs can be checked alongside the cron tool linked below, and validation patterns are often paired with input strength checks rather than used alone.

Authoritative References

  • ECMA-262 (ECMAScript Language Specification) defines the RegExp object, its flags, exec(), lastIndex and replacement string syntax used by this tool.
  • ECMAScript 2018 introduced lookbehind assertions, the s (dotAll) flag and Unicode property escapes.
  • Unicode Standard Annex 44 defines the Unicode character properties referenced by \p{...} escapes in Unicode mode.
  • RFC 5322 defines the Internet message format and the full grammar for mailbox addresses, which is why a short email regex is only a rough filter.

These standards are named as plain text for attribution; no outbound links are placed in this tool.

Key Takeaways

  • Use g for all matches, i for case, m for line anchors, s for newlines and u for Unicode code points.
  • Capture groups are numbered from 1 in the order their opening parenthesis appears, and $1 refers to group 1 in a replacement.
  • Greedy quantifiers take the longest match; add ? to make them lazy.
  • A regex checks the shape of text, not the validity of the underlying value.
  • This tester runs locally, keeps your text private and exports nothing until you click a copy button.

Last updated: December 4, 2026 · All matching runs locally in your browser. No pattern or text is stored or transmitted.

Regex Tester FAQ

What is a regular expression?

A regular expression is a compact pattern that describes a set of strings, letting you find, validate, split or replace text without writing a parser by hand.

Which regex flavor does this tester use?

This tester uses the ECMAScript RegExp engine built into your browser, as specified in ECMA-262, so results match JavaScript and TypeScript code.

What does the g flag do?

The g flag makes the search global, so every non-overlapping match is returned instead of only the first one.

What is the difference between greedy and lazy quantifiers?

A greedy quantifier such as .* takes the longest possible match, while adding a question mark to make .*? takes the shortest possible match.

How do I capture part of a match?

Wrap the part in parentheses to create a capture group, then refer to it as $1 in a replacement or read it from the groups list in the results table.

Why does my pattern match an empty string?

Patterns built only from anchors, lookaheads or a star quantifier can match zero characters; this tool advances the search position after each zero-width match so the scan cannot stall.

Can a regular expression validate an email address completely?

No practical regular expression validates every address allowed by RFC 5322; use a simple pattern for obvious typos and confirm the address by sending a message.

Is my text uploaded anywhere?

No. The pattern and the test string are processed entirely in your browser, and nothing is sent to a server or saved.