Regex Cheatsheet

JavaScript Regular Expression cheatsheet with all common regex syntax and examples

Normal Characters

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
. or [^\n\r]any character excluding a newline or carriage return
[A-Za-z]alphabet
[a-z]lowercase alphabet
[A-Z]uppercase alphabet
\d or [0-9]digit
\D or [^0-9]non-digit
_underscore
\w or [A-Za-z0-9_]alphabet, digit or underscore
\W or [^A-Za-z0-9_]inverse of \w
\Sinverse of \s

Whitespace Characters

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
space
\ttab
\nnewline
\rcarriage return
\sspace, tab, newline or carriage return

Character Set

Think of a character set as an OR operation on the single characters that are enclosed between the square brackets. Use ^ after the opening [ to "negate" the character set. Within a character set, . means a literal period.

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
[xyz]either x, y or z
[^xyz]neither x, y nor z
[1-3]either 1, 2 or 3
[^1-3]neither 1, 2 nor 3

Characters that require escaping

Outside a character set

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
\.period
\^caret
\$dollar sign
\|pipe
\\back slash
\/forward slash
\(opening bracket
\)closing bracket
\[opening square bracket
\]closing square bracket
\{opening curly bracket
\}closing curly bracket

Inside a character set

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
\\back slash
\]closing square bracket

Quantifiers

The quantifier goes after the expression to be quantified.

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
{2}exactly 2
{2,}at least 2
{2,7}at least 2 but no more than 7
*0 or more
+1 or more
?exactly 0 or 1

Boundaries

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
^start of string
$end of string
\bword boundary

How word boundary matching works:

  • At the beginning of the string if the first character is \w
  • Between two adjacent characters within the string, if the first character is \w and the second character is \W
  • At the end of the string if the last character is \w

Matching

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
foo|barmatch either foo or bar
foo(?=bar)match foo if it's before bar
foo(?!bar)match foo if it's not before bar
(?<=bar)foomatch foo if it's after bar
(?<!bar)foomatch foo if it's not after bar

Grouping and Capturing

Capturing groups are only relevant in the following methods: string.match(regexp), string.matchAll(regexp), string.replace(regexp, callback). \N is a backreference to the Nth capturing group. Capturing groups are numbered starting from 1.

ExpressionJavaScript Regular Expression cheatsheet with all common regex syntax and examples
(foo)capturing group; match and capture foo
(?:foo)non-capturing group; match foo but without capturing foo
(foo)bar\1\1 is a backreference to the 1st capturing group; match foobarfoo

Complex Examples

Email Validation

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Validates standard email address format, supports multiple domains and subdomains

Matches: user@example.com, test.email+tag@domain.co.uk
No Match: invalid.email, @domain.com, user@

Phone Number

^(\+?1[-.\s]?)?\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$

Matches US phone number format with various separators and optional country code

Matches: (555) 123-4567, +1-555-123-4567, 555.123.4567
No Match: 123-456, 555-123-45678

Password Strength

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

Requires at least 8 characters with uppercase, lowercase, digits and special characters

Matches: MyPass123!, SecureP@ss1
No Match: password, 12345678, MyPassword

URL Extraction

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

Matches HTTP/HTTPS URLs with subdomains, paths and query parameters

Matches: https://www.example.com, http://sub.domain.org/path
No Match: example.com, ftp://server.com

Date Format

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

Matches MM/DD/YYYY date format with month and day validation

Matches: 12/25/2023, 01/01/2024, 06/15/2023
No Match: 13/25/2023, 1/1/2023, 2023-12-25

Credit Card

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3[0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$

Validates major credit card brand formats (Visa, MasterCard, Amex, etc.)

Matches: 4111111111111111, 5555555555554444
No Match: 1234567890123456, 411111111111111

References and Tools

Online Tools

Documentation