Programming

Regex Cheatsheet

Regular expressions match patterns in text. This cheatsheet covers the syntax for characters, quantifiers, groups, and anchors.

Regular expressions (regex) describe patterns for searching and manipulating text. This cheatsheet covers the core syntax used across most languages.

Character Classes

Match categories of characters.

.        any character (except newline)
\d       a digit (0-9)
\D       a non-digit
\w       word character (a-z, A-Z, 0-9, _)
\W       non-word character
\s       whitespace
\S       non-whitespace

Custom Sets

Define your own character groups.

[abc]      a, b, or c
[^abc]     not a, b, or c
[a-z]      any lowercase letter
[0-9]      any digit
[A-Za-z]   any letter

Quantifiers

Specify how many times.

*        zero or more
+        one or more
?        zero or one (optional)
{3}      exactly 3
{2,4}    between 2 and 4
{2,}     2 or more

Anchors

Match positions, not characters.

^        start of string/line
$        end of string/line
\b       word boundary
\B       non-word boundary

Groups & Alternation

Capture and choose.

(abc)      capture group
(?:abc)    non-capturing group
(a|b)      a or b
\1         backreference to group 1

Common Patterns

Ready-to-use examples.

\d{3}-\d{4}                    phone: 555-1234
[\w.-]+@[\w.-]+\.\w+           basic email
^https?://                       starts with http(s)
\b\d{4}-\d{2}-\d{2}\b          date: 2026-01-31

Flags

Change matching behavior.

g        global (all matches)
i        case-insensitive
m        multiline (^ and $ per line)
s        dotall (. matches newline)

In JavaScript

Use regex in code.

const re = /\d+/g;
"a1b2".match(re);          // ['1', '2']
"a1b2".replace(re, "#");   // 'a#b#'
/^\d+$/.test("123");       // true

Regex is a compact, powerful tool for text processing. Start with character classes and quantifiers, and test patterns interactively before using them.

For an interactive tester, see https://regex101.com/

Promote your content

Reach over 400,000 developers and grow your brand.

Join our developer community

Hang out with over 4,500 developers and share your knowledge.