You are on page 1of 1

JavaScript Regex Cheatsheet

Basics Character Classes


. any character except newline \w \d \s word digit whitespace
a the character a \W \D \S not word digit whitespace
ab the string ab [abc] any of a, b, or c
a|b a or b [^abc] not a, b, or c
a* 0 or more a’s [a-g] characters between a & g
\ escapes a special character
Anchors
Groups & Lookaround ^abc$ start / end of the string
(abc) capture group \b \B word, not-word boundary
\1 backreference to group #1
(?:abc) non-capturing group Escaped Characters
(?=abc) positive lookahead \. \* \\ escaped special characters
(?!abc) negative lookahead \t \n \r tab, linefeed, carriage return

Quantifiers & Alt Common Examples


a* a+ a? 0 or more, 1 or more, 0 or 1 ^\d+$ whole numbers

a{5} a{2,} exactly five, two or more ^[a-zA-Z0-9]*$


alphanumeric with space
a{1,3} between one & three
a+? a{2,}? match as few as possible /\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/i
email address
ab|cd match ab or cd
^(https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6}|[
Flags \d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?$
url validation
g global
i case insensitive
m multiline

codeSTACKr.com

You might also like