Regex tester and cheat sheet

Test a regular expression against sample text and see every match highlighted as you type, using JavaScript's regex engine.

e.g. "i" (case-insensitive)

Match found
Hey, check out this free nitro giveaway: discord.gg/abc123

Highlighted text shows what the pattern matched.

A regex is a pattern that describes text to match rather than one exact string, which makes it useful for moderation filters such as Better Tickets auto-jail triggers. Those triggers run on the same engine as this tester.

How to use the regex tester

1

Enter a pattern

Type your regular expression in Pattern, without surrounding slashes.

2

Set flags

Add flags such as i for case-insensitive matching. The flags are listed below.

3

Paste test text

Paste a sample message, ideally one you want to match and one you don't.

4

Read the result

Matches are highlighted live. An invalid pattern shows the error instead.

How regular expressions work

A pattern is built from a small set of building blocks, combined together.

Literal characters

Most characters just match themselves. The pattern cat matches the text cat wherever it appears.

Character classes

A character class matches one character out of a set.

SyntaxMatches
[abc]a, b, or c
[^abc]Any character except a, b, or c
[a-z]Any lowercase letter
\dAny digit ([0-9])
\wAny word character ([A-Za-z0-9_])
\sAny whitespace character
\D, \W, \SThe negation of \d, \w, \s
.Any character except a newline

Quantifiers

A quantifier says how many times the thing before it can repeat.

SyntaxMeaning
*Zero or more
+One or more
?Zero or one (optional)
{3}Exactly 3
{2,4}Between 2 and 4
{2,}2 or more

Quantifiers are greedy by default: they match as much as possible before backing off. Adding ? after a quantifier (*?, +?) makes it lazy instead, matching as little as possible.

Anchors and boundaries

SyntaxMeaning
^Start of the string (or line, with the m flag)
$End of the string (or line, with the m flag)
\bA word boundary: the edge between a word character and a non-word character
\BNot a word boundary

\bcat\b matches cat as its own word, not the cat inside catalog.

Groups and alternation

Parentheses () group part of a pattern together, so a quantifier can apply to the whole group and so the matched text can be extracted separately. The pipe | inside or around a group means "or": cat|dog matches either cat or dog.

Escaping

Characters like . * + ? ( ) [ ] { } ^ $ | are special to regex. To match one of them literally, put a backslash in front of it: \. matches a literal period, \( matches a literal opening parenthesis.

Flags

Flags go after the pattern and change how the whole match runs.

FlagNameEffect
dindicesAdds match indices to results
gglobalFinds all matches instead of stopping at the first
iignoreCaseCase-insensitive matching
mmultiline^/$ match at the start/end of each line, not just the whole string
sdotAll. also matches newlines
uunicodeTreats the pattern as a sequence of Unicode code points
vunicodeSetsNewer, stricter version of u
ystickyMatches only starting at the current position

Example patterns

Alternation
txt
cat|dog|bird

Some patterns can run very slowly on certain input, particularly nested repetition like (a+)+b. If a pattern needs to run against arbitrary user-supplied text, test it against a long, unusual input in the tester above before relying on it.

Frequently asked questions

Which regex engine does this tester use?

Your browser's JavaScript engine. Better Tickets auto-jail triggers run on JavaScript too, so a pattern that behaves here behaves the same way there.

How do I make a pattern case-insensitive?

Add the i flag. free nitro with i matches "Free Nitro", "FREE NITRO" and every mix in between.

How do I match a whole word only?

Wrap it in word boundaries: \bscam\b matches "scam" but not "scamper".

Why does my pattern match too much?

Quantifiers like .* are greedy and grab as much as they can. Make them lazy with ? (.*?), anchor the pattern with ^ and $, or use a narrower class such as \S+ instead of .*.

Can a regex be slow?

Yes. Nested repetition such as (a+)+b can take a very long time on some inputs. Test patterns against long, unusual text before using them on messages from the public.