JavaScript Regular Expression Cheat Sheet

JavaScript Regular Expression Cheat Sheet

A list of regex expressions for quick access
Ferenc Almasi β€’ 2022 April 28 β€’ Read time 1 min read
  • twitter
  • facebook
JavaScript

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.


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)
Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
Master JavaScriptinfo Remove ads

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
Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
Master JavaScriptinfo Remove ads

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
  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.