JavaScript: Regular Expressions Cheat Sheet

Updated: March 1, 2023 By: Khue Post a comment

Here’s a comprehensive cheat sheet about regular expressions in JavaScript, to help you quickly look up the most important things. I also use this cheat sheet very often when working on my projects. You can bookmark this page for later use when needed.

Creating a Regular Expression

To create a regular expression in JavaScript, you can use either a forward slash notation or the RegExp constructor:

// Using forward slash notation
var regex = /pattern/;

// Using the RegExp constructor
var regex = new RegExp('pattern');

Flags

Flags are optional modifiers that can be added to a regular expression to modify its behavior. There are 3 main flags in JavaScript:

// Global flag (match all occurrences)
var regex = /pattern/g;

// Case-insensitive flag
var regex = /pattern/i;

// Multiline flag
var regex = /pattern/m;

Functions

Regular expressions can be used in several functions in JavaScript to match and manipulate text:

// Test a string for a match
regex.test(str);

// Search a string for a match
str.search(regex);

// Replace matches in a string
str.replace(regex, replacement);

// Extract matched values from a string
str.match(regex);

// Split a string by matches
str.split(regex);

Character Classes

Character classes are used to match specific characters or ranges of characters:

// Match any single character
.

// Match any digit character
\d

// Match any non-digit character
\D

// Match any word character (letter, digit, or underscore)
\w

// Match any non-word character
\W

// Match any whitespace character
\s

// Match any non-whitespace character
\S

// Match any character in a specific set
[abc]

// Match any character not in a specific set
[^abc]

// Match any character in a range
[a-z]

// Match any character not in a range
[^a-z]

Quantifiers

Quantifiers are used to match multiple occurrences of a character or group:

// Match zero or one of the preceding character
?

// Match zero or more of the preceding character
*

// Match one or more of the preceding character
+

// Match exactly n of the preceding character
{n}

// Match n or more of the preceding character
{n,}

// Match between n and m of the preceding character
{n,m}

// Match as much as possible (greedy)
?

// Match as little as possible (lazy)
?? 

Anchors

Anchors are used to match specific positions in a string:

// Match the start of a string
^

// Match the end of a string
$

// Match a word boundary
\b

// Match a non-word boundary
\B

Groups

Groups are used to group together parts of a regular expression and apply quantifiers or other modifiers to them:

// Match any of the characters in the group
[abc]

// Match any character not in the group
[^abc]

// Match any character in a range
[a-z]

// Match any character not in a range
[^a-z]

// Match the group as a subexpression
(pattern)

// Match either the left or right subexpression
(left|right)

// Create a non-capturing group
(?:pattern)

// Match the preceding group n times
(pattern){n}

// Match the preceding group n or more times
(pattern){n,}

// Match the preceding group between n and m times
(pattern){n,m}

Special Characters

There are several special characters that have special meanings in regular expressions:

// Match any whitespace character
\s

// Match any non-whitespace character
\S

// Match the start of a string
^

// Match the end of a string
$

// Match any character except a newline
.

// Match zero or one of the preceding character
?

// Match zero or more of the preceding character
*

// Match one or more of the preceding character
+

// Match the preceding character n times
{ n }

// Match the preceding character n or more times
{ n, }

// Match the preceding character between n and m times
{ n, m }

// Match the left or right subexpression
( left | right )

// Create a non-capturing group
(?: pattern )

// Match the group as a subexpression
( pattern )

// Match the previous match
\ n

Lookahead and Lookbehind

Lookahead and lookbehind assertions are used to match a pattern only if it is followed or preceded by another pattern, without including the matched text in the final result:

// Positive lookahead
(?=pattern)

// Negative lookahead
(?!pattern)

// Positive lookbehind
(?<=pattern)

// Negative lookbehind
(?<!pattern)

Final Words

Hope this cheat sheet is helpful to you. If you see anything that needs to be added or corrected, let me know by leaving comments.

If you need more detailed instructions and more concrete examples about JavaScript regular expressions, continue reading other articles in this series, such as these ones:

Happy coding & have a nice day!