Helper-KitCheatsheets

Regex Cheatsheet

Regular expression syntax with tested examples: classes, quantifiers, anchors, groups, lookarounds, flags and common patterns.

Regex Cheatsheet · Helper-Kit

Examples use JavaScript syntax. Most of it works the same in Python, Java, C#, PHP and Go; lookbehind and named groups vary slightly.

45 entries

Characters

Any character

Matches any single character except a line break.

.

/c.t/ matches "cat", "cot" and "c9t"

With the s flag, . also matches line breaks.

One of these characters

Matches a single character from the set.

[abc]

/gr[ae]y/ matches "gray" and "grey"

Not one of these

Matches a single character that is not in the set.

[^abc]

/[^0-9]/ matches the first non-digit

Range

Matches a character in the range. Ranges can be combined.

[a-z]  [A-Z]  [0-9]  [a-zA-Z0-9]

/[A-F0-9]/ matches one hex digit (uppercase)

Digit / not a digit

\d is the same as [0-9].

\d  \D

/\d+/ matches "2026" in "Year 2026"

Word character / not a word character

\w is the same as [A-Za-z0-9_]. It does not include accented letters.

\w  \W

/\w+/ matches "user_1" in "user_1@site"

Whitespace / not whitespace

Space, tab, line break and other spacing characters.

\s  \S

/\s+/ matches the gap in "a b"

Tab, line breaks

Tab, new line (Unix), carriage return + new line (Windows).

\t  \n  \r\n

Unicode letters

Any letter in any language. Needs the u flag.

\p{L}

/\p{L}+/u matches "café" and "Ålesund"

Quantifiers

Zero or more

The previous item, any number of times (including none).

*

/ab*c/ matches "ac", "abc", "abbbc"

One or more

The previous item at least once.

+

/ab+c/ matches "abc", "abbc" but not "ac"

Optional

The previous item zero or one time.

?

/colou?r/ matches "color" and "colour"

Exact count or range

Exactly n, at least n, or between n and m times.

{n}  {n,}  {n,m}

/\d{3}-\d{4}/ matches "555-1234"

Lazy (as few as possible)

Add ? after a quantifier to stop at the first possible match.

*?  +?  ??  {n,m}?

/<.+?>/ matches "<b>" in "<b>bold</b>", not the whole string

Anchors

Start / end

Start and end of the text. With the m flag: start and end of each line.

^  $

/^\d+$/ matches only if the whole text is digits

Word boundary

The edge between a word character and a non-word character.

\b  \B

/\bcat\b/ matches "cat" in "a cat sat" but not in "concat"

Groups

Either / or

Matches the left side or the right side.

a|b

/cat|dog/ matches "cat" or "dog"

Capture group

Groups part of the pattern and saves what it matched.

(abc)

/(\d{4})-(\d{2})/ on "2026-10" captures "2026" and "10"

Group without capturing

Groups for quantifiers or | without saving the match.

(?:abc)

/(?:ha)+/ matches "hahaha"

Named group

A capture group you can refer to by name.

(?<name>abc)

/(?<year>\d{4})/ → match.groups.year

Back-reference

Matches the same text a group matched earlier.

\1  \k<name>

/(\w)\1/ matches "ll" in "hello"

In replacements

Insert captured groups into the replacement text.

$1  $<name>  $&

"2026-10-13".replace(/(\d+)-(\d+)-(\d+)/, "$3/$2/$1") → "13/10/2026"

Lookarounds

Followed by

Matches only if followed by the pattern, without including it.

x(?=y)

/\d+(?=px)/ matches "16" in "16px"

Not followed by

Matches only if not followed by the pattern.

x(?!y)

/\d+(?!px|\d)/ matches "2" in "2em", not "16" in "16px"

Preceded by

Matches only if preceded by the pattern.

(?<=y)x

/(?<=\$)\d+/ matches "25" in "$25"

Not preceded by

Matches only if not preceded by the pattern.

(?<!y)x

/(?<!\$)\b\d+/ matches "3" in "$25 for 3"

Flags

Global

Find every match, not only the first.

/pattern/g

Ignore case

Uppercase and lowercase match each other.

/pattern/i

Multiline

^ and $ match at the start and end of every line.

/pattern/m

Dot matches line breaks

Lets . match \n too.

/pattern/s

Unicode

Correct handling of emoji and other characters, and enables \p{…}.

/pattern/u

Escaping

Special characters

Put a backslash before these to match them literally.

\.  \*  \+  \?  \^  \$  \(  \)  \[  \]  \{  \}  \|  \\  \/

/example\.com/ matches "example.com" but not "exampleXcom"

Slash in a regex literal

In /…/ literals, escape the slash. In new RegExp("…") strings, double the backslashes instead.

/https?:\/\//
new RegExp("\\d+")

Common patterns

Email (simple check)

Something@something.something. Good for catching typos, not for full validation.

^[^\s@]+@[^\s@]+\.[^\s@]+$

The only real test of an email address is sending a message to it.

URL

http or https links.

https?:\/\/[\w.-]+(?:\.[a-z]{2,})(?:[\/?#][^\s]*)?

matches "https://example.com/page?x=1"

Date (YYYY-MM-DD)

Checks the shape and month/day ranges, not real dates like 31 February.

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Time (24-hour HH:MM)

00:00 to 23:59.

^([01]\d|2[0-3]):[0-5]\d$

Hex colour

#fff or #ffffff.

^#(?:[0-9a-fA-F]{3}){1,2}$

IPv4 address

Four numbers from 0 to 255.

^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$

Strong password

At least 8 characters with a lowercase letter, an uppercase letter and a digit.

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Extra spaces

Two or more spaces in a row, or spaces at the start or end of a line.

 {2,}
^\s+|\s+$

In JavaScript

Test for a match

Returns true or false.

/^\d+$/.test("123")  // true

Get matches

The first match with groups, or every match with the g flag.

"a1b22".match(/\d+/)       // ["1", …]
"a1b22".match(/\d+/g)      // ["1", "22"]
[..."a1b22".matchAll(/\d+/g)]

Replace

Replace the first match, or all of them with g.

"a-b-c".replace(/-/g, "_")   // "a_b_c"

Split

Split text on a pattern.

"a, b;c".split(/[,;]\s*/)  // ["a", "b", "c"]

💡 Search matches titles, descriptions and the commands themselves. Copy puts the exact text on your clipboard; Print gives a clean sheet without menus or buttons. Searching happens in your browser; nothing is sent anywhere.

About This Cheatsheet

Regular expressions (regex) describe text patterns, so you can find, check or replace text: every email address in a file, dates in a certain format, or extra spaces. This cheatsheet covers characters and classes, quantifiers, anchors, groups and back-references, lookarounds, flags, escaping, ready-made patterns and how to use regex in JavaScript. Every example shows a pattern and what it matches, and every example on this page has been tested. The syntax is JavaScript's, which is shared by most languages; lookbehind and named groups vary slightly between them. To try a pattern on your own text, open it in the Regex Tester.

Quick Tips

  • Start simple and add one piece at a time, testing as you go.
  • Use ^ and $ when the whole value must match, for example in form validation.
  • Add ? after * or + (lazy) when a match swallows too much.
  • Escape special characters such as . ? + ( ) with a backslash when you mean them literally.
  • Use regex to catch typos, not as the only check: an email pattern cannot prove an address exists.

Frequently Asked Questions

What is the difference between * and +?

* matches the previous item zero or more times, so it can match nothing. + needs at least one.

Why does my pattern match too much?

Quantifiers are greedy and take as much as they can. Add ? to make them lazy: <.+?> stops at the first >.

How do I make a regex ignore case?

Add the i flag: /hello/i matches "Hello" and "HELLO". In new RegExp() pass "i" as the second argument.

What is a capture group?

Brackets ( ) group part of a pattern and save what it matched, so you can read it later or use it in a replacement as $1, $2 and so on.

Was this tool helpful?

More in this category