JavaScript Regular Expression Cheat Sheet

Regular expressions can be hard to remember, no wonder you have landed on this page. Below you will find a full list of regular expressions you can use in JavaScript with their meaning explained.

JavaScript Course

Flags

FlagExplanation
gMatch globally
iCase insensitive matching
mMatch on multiple lines when using anchors
s (dotall)Makes . to match new lines as well
uUnicode support

Anchors

RegexExplanation
^aStarts with the next character (a)
z$Ends with the preceding character (z)

Character Classes

RegexExplanation
\wMatch for word
\dMatch for digit
\sMatch for whitespace
\WMatch any character that is not a word
\DMatch any character that is not a digit
\SMatch any character that is not a whitespace
[az]Match any of "a" and "z"
[^az]Match any character that is not "a" and "z"
[a-z]Match a set of characters inside the brackets (a to z)
.Match everything expect a new line

Quantifiers and Alternations

RegexExplanation
a{4}Match preceding token that is exactly 4 characters long
a{2,4}Match preceding token that is between 2 and 4 characters long
a{2,}Match preceding token longer than 2 characters
a*Match 0 or more of the preceding character
a+Match 1 or more of the preceding character
a?Match 0 or 1 of the preceding character
a|zMatch “a” or “z”

Escaped Characters

RegexExplanation
\/Escape a forward slash (char code 47)
\\Escape a backslash (char code 92)
\.Escape a dot (char code 46)
\*Escape an asterisk
\tMatch tabs
\nMatch new line
\rMatch carriage return

Groups and Lookarounds

RegexExplanation
(2020)
Capturing group - group the expression when there is a match
(?:2020)Non-capturing group - don't capture the expression
(?<year>2020)Named capturing group - name the captured group as "year"
(?=2020)Positive lookahead - see if pattern is followed by the expression
(?!2020)Negative lookahead - see if pattern is not followed by the expression
(?<=2020)Positive lookbehind - see if pattern is preceded by the expression
(?<!2020)Negative lookbehind - see if pattern is not preceded by the expression

JavaScript Functions

FunctionReturn value
regex.exec('string')Returns null or array containing the match  
regex.test('string')Returns true or false based on the provided string  
string.match(/regex/g)Returns null or array containing matches  
string.matchAll(/regex/g)Returns an empty array or RegExpStringIterator
string.search(/regex/g)Returns index, returns -1 if no match is found  
string.replace(/regex/g, 'newString')Returns a string where the matched expression is replaced with the second argument
string.replaceAll(/regex/g, 'newString')Returns a string where all occurrences of the matched expression are replaced with the second argument
string.split(/regex/g)Breaks the string into an array of substrings based on the expression

Tools

SiteDescription
regexr.comTest your regular expressions interactively
regexper.comVisualize your regular expressions
JavaScript Course
Remove ads
Remove ads

📚 Get access to exclusive content

Want to get access to exclusive content? Support webtips with the price of a coffee to get access to tips, checklists, cheatsheets, and much more. ☕

Get access Support us
Remove ads Read more on
Remove ads
Remove ads
🎉 Thank you for subscribing to our newsletter. x