ZenovayTools

Regex Cheatsheet

Interactive regex reference with searchable syntax guide. Test patterns with live matching. Covers character classes, quantifiers, groups, and flags.

Showing 39 of 39 entries

Character Classes

10 entries
.

Any character

Matches any single character except newline (unless s flag is set)

Pattern: c.tMatches: "cat", "cut", "c3t"
\d

Digit

Matches any digit character [0-9]

Pattern: \d+Matches: "42", "007"
\D

Non-digit

Matches any character that is not a digit

Pattern: \D+Matches: "abc", "foo!"
\w

Word character

Matches any alphanumeric character or underscore [A-Za-z0-9_]

Pattern: \w+Matches: "hello", "foo_42"
\W

Non-word character

Matches any character that is not a word character

Pattern: \W+Matches: " ", "!@#"
\s

Whitespace

Matches any whitespace character: space, tab, newline, carriage return, form feed

Pattern: \s+Matches: " ", "\t\n"
\S

Non-whitespace

Matches any character that is not whitespace

Pattern: \S+Matches: "hello", "word"
[abc]

Character set

Matches any single character listed inside the brackets

Pattern: [aeiou]Matches: "a", "e", "i", "o", "u"
[^abc]

Negated set

Matches any single character NOT listed inside the brackets

Pattern: [^aeiou]Matches: "b", "c", "d" (not vowels)
[a-z]

Character range

Matches any character in the specified Unicode range. Combine ranges: [a-zA-Z0-9]

Pattern: [a-z]+Matches: "hello", "world"

Anchors

4 entries
^

Start of string

Matches the position at the start of the string (or line with m flag)

Pattern: ^HelloMatches: "Hello world" (start only)
$

End of string

Matches the position at the end of the string (or line with m flag)

Pattern: world$Matches: "Hello world" (end only)
\b

Word boundary

Matches the position between a word character and a non-word character

Pattern: \bcat\bMatches: "cat" but not "catfish"
\B

Non-word boundary

Matches any position that is not a word boundary

Pattern: \Bcat\BMatches: "concatenate" (mid-word)

Quantifiers

8 entries
*

Zero or more (greedy)

Matches the preceding element zero or more times, as many as possible

Pattern: ab*cMatches: "ac", "abc", "abbc"
+

One or more (greedy)

Matches the preceding element one or more times, as many as possible

Pattern: ab+cMatches: "abc", "abbc" (not "ac")
?

Zero or one

Matches the preceding element zero or one time (makes it optional)

Pattern: colou?rMatches: "color", "colour"
{n}

Exactly n

Matches the preceding element exactly n times

Pattern: \d{4}Matches: "2024", "1234"
{n,}

n or more

Matches the preceding element n or more times

Pattern: \d{2,}Matches: "12", "999", "1000"
{n,m}

Between n and m

Matches the preceding element between n and m times (inclusive)

Pattern: \d{2,4}Matches: "12", "123", "1234"
*?

Zero or more (lazy)

Matches the preceding element zero or more times, as few as possible

Pattern: <.+?>Matches: "<b>" in "<b>bold</b>"
+?

One or more (lazy)

Matches the preceding element one or more times, as few as possible

Pattern: "[^"]+?"Matches: Shortest quoted string

Groups

6 entries
(abc)

Capturing group

Groups the expression and captures the matched text. Accessible as \1, \2, or match[1]

Pattern: (\d{4})Matches: Captures year in "2024-01-15"
(?:abc)

Non-capturing group

Groups the expression without creating a capture. Use to apply quantifiers without capturing

Pattern: (?:ab)+Matches: "ab", "abab", "ababab"
(?=abc)

Positive lookahead

Asserts that what follows the current position matches the pattern (zero-width)

Pattern: \d+(?= USD)Matches: "100" in "100 USD"
(?!abc)

Negative lookahead

Asserts that what follows does NOT match the pattern (zero-width)

Pattern: \d+(?! USD)Matches: "42" in "42 EUR"
(?<=abc)

Positive lookbehind

Asserts that what precedes the current position matches the pattern (zero-width)

Pattern: (?<=\$)\d+Matches: "100" in "$100"
(?<!abc)

Negative lookbehind

Asserts that what precedes does NOT match the pattern (zero-width)

Pattern: (?<!\$)\d+Matches: "42" but not "$42"

Flags

6 entries
g

Global

Find all matches rather than stopping after the first match

Pattern: /\d+/gMatches: All numbers in a string
i

Ignore case

Makes the pattern case-insensitive

Pattern: /hello/iMatches: "hello", "HELLO", "Hello"
m

Multiline

Makes ^ match the start of each line and $ match the end of each line

Pattern: /^\w+/mMatches: First word of each line
s

DotAll

Makes . match any character including newline characters

Pattern: /a.b/sMatches: "a\nb" across newlines
u

Unicode

Enables full Unicode matching; required for \p{...} property escapes and 4-byte Unicode code points

Pattern: /\p{L}+/uMatches: Any Unicode letter
y

Sticky

Matches only from the position indicated by lastIndex; does not advance past it

Pattern: /\d+/yMatches: Only at exact lastIndex position

Special

5 entries
|

Alternation

Matches either the expression before or after the pipe. Acts like OR

Pattern: cat|dogMatches: "cat" or "dog"
\1

Backreference

Matches the same text that was matched by capturing group 1. \2 for group 2, etc.

Pattern: (\w+)\s\1Matches: "the the", "ha ha"
\n

Newline

Matches a newline character (line feed, LF)

Pattern: \r?\nMatches: Unix or Windows line endings
\t

Tab

Matches a horizontal tab character

Pattern: \t+Matches: Tab-indented lines
\\

Literal backslash

Matches a literal backslash character. Backslash must be escaped as \\

Pattern: C:\\\w+Matches: "C:\Users" in file paths

Quick Test

— paste a pattern and see matches highlighted
//g

How to Use Regex Cheatsheet

  1. 1Browse regex syntax by category.
  2. 2Search for specific patterns or concepts.
  3. 3Test your regex with the live matcher.
  4. 4Copy any pattern to use in your code.
Zenovay

Privacy-first analytics for your website

Understand your visitors without invasive tracking. GDPR compliant, lightweight, and powerful.

Explore Zenovay

Frequently Asked Questions

What is a regular expression?
A regular expression (regex) is a sequence of characters that defines a search pattern. It is used for matching, finding, and manipulating text. For example, \d+ matches one or more digits, and [A-Za-z]+ matches one or more letters. Regex engines are built into virtually every programming language and text editor.
What are the different regex flavors?
Different languages implement regex with varying syntax and capabilities. JavaScript (ECMAScript) is the flavor used in browsers and Node.js — it lacks some features like named backreferences in lookbehinds in older engines. PCRE (Perl-Compatible Regular Expressions) is used by PHP, Python, Ruby, and many others and is the most feature-rich. POSIX (Basic and Extended) is used in Unix tools like grep and sed and has a more limited syntax. This cheatsheet covers ECMAScript/JavaScript regex as implemented in modern browsers.
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +, ?, {n,m}) match as much text as possible while still allowing the overall pattern to match. Lazy (non-greedy) quantifiers (*?, +?, ??, {n,m}?) match as little text as possible. For example, given the string "<b>bold</b>", the greedy pattern <.+> matches the entire string "<b>bold</b>", while the lazy pattern <.+?> matches only "<b>" — the shortest possible match.
What are lookahead and lookbehind assertions?
Lookahead and lookbehind are zero-width assertions — they check for a pattern without consuming characters. Positive lookahead (?=abc) asserts that what follows is "abc". Negative lookahead (?!abc) asserts that what follows is not "abc". Positive lookbehind (?<=abc) asserts that what precedes is "abc". Negative lookbehind (?<!abc) asserts that what precedes is not "abc". Example: \d+(?= dollars) matches numbers only when followed by " dollars".
What are the most common regex mistakes?
The most frequent mistakes include: (1) Forgetting to escape special characters like . + * ? ( ) [ ] { } \ ^ $ | — a bare . matches any character, not a literal dot. (2) Using greedy quantifiers when lazy is needed, causing over-matching. (3) Anchoring issues — forgetting ^ and $ means the pattern can match anywhere in the string. (4) Catastrophic backtracking: patterns like (a+)+ on long non-matching input can cause exponential slowdowns. (5) Assuming . matches newlines — by default it does not; use the s (dotAll) flag to change this.