Skip to content
HandyKit

Regex Tester

Write a regular expression and see every match highlighted as you type, with the pattern explained part by part, capture groups, replacement and code you can paste into JavaScript.

Runs in your browser. Your input is never uploaded or stored.

Flags

Checks the format and ranges, not real calendar dates such as 30 February.

Explanation

  1. \bA word boundary (between a word character and a non-word character)
  2. (?<year>Capturing group 1, named “year”:
  3. \d{4}A digit (0–9), exactly 4 times
  4. -The character “-”
  5. (?<month>Capturing group 2, named “month”:
  6. 0The character “0”
  7. [1-9]One character from: 1–9
  8. |Or
  9. 1The character “1”
  10. [0-2]One character from: 0–2
  11. -The character “-”
  12. (?<day>Capturing group 3, named “day”:
  13. 0The character “0”
  14. [1-9]One character from: 1–9
  15. |Or
  16. [12]One character from: “1”, “2”
  17. \dA digit (0–9)
  18. |Or
  19. 3The character “3”
  20. [01]One character from: “0”, “1”
  21. \bA word boundary (between a word character and a non-word character)
Quick reference
.
Any character except a line break
\d \w \s
Digit, word character, whitespace
\D \W \S
Not a digit, word character, whitespace
[abc]
One of a, b or c
[^abc]
Any character except a, b or c
[a-z]
One character from a to z
^ $
Start, end of text (of line with m)
\b
Word boundary
a* a+ a?
0 or more, 1 or more, 0 or 1
a{3} a{3,} a{3,6}
Exactly 3, 3 or more, 3 to 6
a+?
Lazy: as few as possible
(abc)
Capturing group
(?<name>abc)
Named group
(?:abc)
Group without capturing
a|b
a or b
\1 \k<name>
Same text as group 1, or as “name”
a(?=b) a(?!b)
a followed by b, not followed by b
(?<=b)a (?<!b)a
a preceded by b, not preceded by b
\. \* \\
A literal ., * or \
\p{L}
Any letter, in any script (with u)

Matches appear here.

What is a regular expression?

A regular expression (regex) is a pattern that describes text. \d{4}-\d{2}-\d{2} describes a date such as 2026-10-20; ^\s+|\s+$ describes whitespace at the start or end of a string. Regexes are built into almost every programming language and editor, and are used to validate input, search logs, extract data and find-and-replace across files.

How to test a regex

  1. Type your pattern in Pattern, without the surrounding slashes, or choose one from Common patterns.
  2. Tick the flags you need. Global finds every match instead of just the first.
  3. Paste sample text into Test string. Matches are highlighted as you type, alternating between two shades so that adjacent matches are easy to tell apart.
  4. Open Matches to see each match’s position and capture groups, Replace to try a replacement, or Code for JavaScript you can paste into your project.

Regex syntax cheat sheet

Characters and classes

  • . any character except a line break (any character at all with the s flag)
  • \d a digit, \w a word character (letter, digit or underscore), \s whitespace; \D, \W, \S are their opposites
  • [abc] one of a, b or c; [^abc] anything else; [a-z] a range
  • \b a word boundary, \. a literal dot

Anchors

  • ^ start of the string (or line, with m), $ end of the string (or line)

Quantifiers

  • * zero or more, + one or more, ? zero or one
  • {3} exactly three, {2,5} two to five, {2,} two or more
  • Add ? to make them lazy, matching as little as possible: .*?

Groups and alternation

  • (…) a capturing group, available as $1 in replacements
  • (?<name>…) a named group, available as $<name>
  • (?:…) a group that does not capture
  • a|b either a or b

Lookaround (match a position without consuming text)

  • (?=…) followed by, (?!…) not followed by
  • (?<=…) preceded by, (?<!…) not preceded by

Catastrophic backtracking

When a match fails, the regex engine backtracks and tries other ways to divide the text between the parts of the pattern. With nested quantifiers such as (a+)+$, the number of ways grows exponentially. Against 30 a characters followed by !, that is over a billion attempts, enough to freeze a browser tab or take down a server. This is the basis of ReDoS (regular expression denial of service) attacks.

This tester runs every pattern in a background worker and stops it after one second, so you can find these patterns safely. To fix one, remove the nesting (a+$ instead of (a+)+$) or make each repeated part unable to match the same text as its neighbour.

Using regular expressions in JavaScript

const date = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;

date.test('Due 2026-10-20');                         // true
'Due 2026-10-20'.match(date).groups.year;             // '2026'
'Due 2026-10-20'.replace(date, '$<day>/$<month>/$<year>'); // 'Due 20/10/2026'

for (const m of 'a1 b22'.matchAll(/\d+/g)) console.log(m[0], m.index);

matchAll and replaceAll need the g flag. When building a pattern from user input with new RegExp(…), escape special characters first, or a stray ( will throw an error.

How JavaScript regex differs from other languages

  • Python writes named groups as (?P<name>…) and refers to them as (?P=name); JavaScript uses (?<name>…) and \k<name>.
  • PCRE (PHP, many command-line tools) has possessive quantifiers such as a++ and atomic groups; JavaScript does not.
  • grep and sed use POSIX syntax by default, where + and ? may need a backslash or the -E option.

Frequently asked questions

Which regex flavour does this tester use?

JavaScript (ECMAScript), using your browser’s own engine, so a pattern that works here works the same in browser and Node.js code. That includes lookbehind, named groups and the Unicode flag. Other languages differ in places: Python writes named groups as (?P<name>…), and PCRE supports possessive quantifiers that JavaScript does not.

Why was my pattern stopped?

It ran for more than a second, which almost always means catastrophic backtracking: nested quantifiers such as (a+)+ give the engine exponentially many ways to try a failing match. The tester runs patterns in a background worker so it can stop them; in real code, the same pattern could freeze a page or a server.

Why does ^ only match at the start of the text?

By default ^ and $ match at the start and end of the whole string. Turn on the Multiline (m) flag to make them match at the start and end of every line.

How do I match a dot, bracket or other special character?

Put a backslash in front of it: \. matches a literal dot, \( a bracket and \$ a dollar sign. The characters that need escaping are . * + ? ^ $ { } ( ) | [ ] \ and, in a JavaScript literal, /.

Is there a regex that validates every email address?

Not a practical one. The full address grammar allows things like quoted local parts that no sensible pattern covers. Use a simple pattern to catch typos, such as the one in the Common patterns list, and confirm the address by sending an email.

Is my text sent anywhere?

No. Patterns run in a Web Worker inside your browser. Nothing you type is uploaded, logged or stored.

Guides