🎄 Get 50% off from our JavaScript course for the holidays! 🎄
JavaScript Regular Expression Cheat Sheet

JavaScript Regular Expression Cheat Sheet

A list of regex expressions for quick access

Ferenc Almasi • 2022 April 28 • 📖 1 min read

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.

Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
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
Share on
  • twitter
  • facebook
JavaScript Course Dashboard

Tired of looking for tutorials?

You are not alone. Webtips has more than 400 tutorials which would take roughly 75 hours to read.

Check out our interactive course to master JavaScript in less time.

Learn More

Recommended

🎉 Thank you for subscribing to our newsletter. x