Skip to content
LeedyBox
Support

RegEx Tester, Replace & Cheat Sheet

Test regular expressions with highlighted matches and capture groups, preview replacements, and copy the pattern as code for six languages.

Flags
Matches: 0

Match details and capture groups

Cheat sheet

Click a pattern to load it with a sample text.

Syntax reference

Characters

.Any character except a line break (with s: including line breaks)
\d \DDigit 0–9 / not a digit
\w \WWord character (A–Z, a–z, 0–9, _) / not one
\s \SWhitespace / not whitespace
[abc] [^abc]One of a, b, c / anything except a, b, c
[a-z]A character in the range a to z
\p{L}A letter in any language (needs the u flag)
\. \\ \(Escape a special character to match it literally

Quantifiers

* + ?0 or more / 1 or more / 0 or 1
{n} {n,} {n,m}Exactly n / n or more / between n and m
*? +? {n,m}?Lazy: match as few characters as possible

Anchors

^ $Start / end of the text (of each line with m)
\b \BWord boundary / not a word boundary

Groups & lookaround

(…)Capturing group, used as $1, $2 …
(?:…)Group without capturing
(?<name>…)Named group, used as $<name>
\1 \k<name>Backreference: the same text a group matched
a|ba or b
(?=…) (?!…)Followed by / not followed by
(?<=…) (?<!…)Preceded by / not preceded by

Replacement

$1 $<name>Text of a numbered or named group
$& $$The whole match / a literal $

Check the results before you use them. The tool is provided as is, without warranty, and its results can be incomplete or wrong. Terms of Service

Patterns are tested with the JavaScript regex engine. Syntax and behaviour can differ in other languages and databases. The cheat-sheet patterns are practical examples, not complete validators.

How to use

  1. Enter the pattern under "Regular expression" without the enclosing slashes and flags, and paste your text into "Test string". Matches are highlighted under "Matches" and counted as you type. To start from an example, click a pattern in the "Cheat sheet"; this replaces the current pattern, flags, test string and replacement settings.
  2. Tick the flags you need under "Flags"; hover over a letter to see what it does. g and u are on when the page opens. Without g, only the first match is found and only the first one is replaced.
  3. Read "Match details and capture groups": each match is listed with its position and the text of every group as $1, $2 and so on, with the name of named groups. Turn on the d flag to also see where each group starts and ends.
  4. To try a substitution, tick "Replace matches with" and type the replacement, using $1 or $<name> for groups. The changed text appears under "Replacement result", and "Copy result" copies it.
  5. Under "Code generator", choose JavaScript, Python, Java, PHP, Go or C# and click "Copy code". The snippet includes the replacement when "Replace matches with" is ticked. "Copy as /pattern/flags" copies the pattern as a JavaScript regex literal.

Frequently asked questions

Is the text I test sent anywhere?

No. The pattern and the test string are matched in your browser by its own JavaScript regex engine, and they are not uploaded to LeedyBox. The tool does not save them either.

Why does my pattern match more text than I expected?

A pattern matches wherever it fits, so \d{3} also finds 123 inside 12345. Add \b at both ends for whole words or numbers, or use ^ and $ to require the whole text to match (each line, with m on), as the password pattern in the cheat sheet does. \b relies on the word characters of \w (A–Z, a–z, 0–9 and _) and treats letters such as é or 한 as non-word characters, so \bcafé\b does not match café when a space follows it. Quantifiers such as + and * are also greedy: on <b>x</b>, the pattern <.+> matches the whole string, while <.+?> or <[^>]+> stops at the first >.

Why does a pattern that works in Python, PHP or Java show a RegEx error here?

This page uses the JavaScript engine, which rejects some syntax from other engines, such as (?P<name>…) named groups, inline flags like (?i) at the start, atomic groups (?>…) and possessive quantifiers such as a++. Write named groups as (?<name>…) and use the checkboxes under "Flags" instead of inline flags. With the u flag, JavaScript is also stricter about escapes: \# is an error, and so is \- outside square brackets, so remove the backslash or turn off u.

Why does \p{L} not match letters, or an emoji count as two characters?

Unicode property classes such as \p{L} need the u flag; without it, JavaScript reads \p{L} as the literal text p{L}, and . matches only one half of a character outside the Basic Multilingual Plane, such as many emoji. Loading a cheat-sheet pattern also loads its flags, and all of them except "Special characters" leave u off, so check "Flags" before you add such syntax. Even with u, . matches one code point, and some emoji, such as flags or a thumbs-up with a skin tone, are made of two or more. Positions in the match details count UTF-16 code units, as JavaScript does, so characters outside the Basic Multilingual Plane take two positions even with u.

Why does \n in the replacement not insert a line break?

The replacement follows the rules of JavaScript's replace(): only sequences that start with $ are special, so \n and \t are inserted as a backslash followed by a letter, not as a line break or a tab. The field holds a single line, so a line break cannot be entered here; add it in the generated code instead. To delete matches, tick "Replace matches with" and leave the field empty, as the "Remove HTML/XML tags" pattern in the cheat sheet does.

What happens if a pattern is very slow or finds a very large number of matches?

When the browser allows Web Workers, matching runs in a background worker and is stopped after 2 seconds, so a pattern with catastrophic backtracking, such as (a+)+$ on a long row of a's followed by another character, shows a message instead of freezing the page. The count under "Matches" includes every match, but only the first 1,000 are highlighted and listed in the details.

Can I paste the generated code into my project as it is?

Use it as a starting point. The generator writes the pattern as a regex literal in JavaScript and as a string in the other languages, rewrites named groups and replacement references where the syntax differs, such as (?P<name>…) and \g<1> in Python, maps the i, m and s flags, and expects your input in a variable called text. If the pattern contains quotes or slashes, check that the snippet escapes them correctly. Engines still differ: Go's regexp package uses RE2 syntax, which does not support lookaround or backreferences, and Python's standard re module does not support \p{…} classes, so test the code with your own data.