Show Menu
Cheatography

Javascript regex Cheat Sheet (DRAFT) by

Javascript regex cheatsheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Methods

regex.t­es­t(s­tring)
returns bool
string.ma­tch­(regex)
returns regex from string w/ extract, index and input
string.re­pla­ce(­regex, input)
replaces regex in string with input (can be function or string)
string.re­pla­ce(­regex, '$a $b')
a & b can be capture groups from regex

Character Classes

/\syntax\
syntax
 
\w
[A-Za-­z0-9_]
\W
[^A-Za­-z0-9_]
\d
[0-9]
\D
[^0-9]
\s
whites­paces and return
\S
NON whites­paces and return
 

Patterns

/regex/
litteral match
/regex­|mo­rer­egex/
Literal match w/ different possib­ilities
/?/
lazy search
/^a/
returns a if first
/a$/
returns a if last
 
/(?=a)/
returns if a is present
/(?!a)/
returns if a is NOT present
 
/./
wildcard character
/a?/
a optional
 
/[a+]/
a or aa as single match (1 or more)
/[Aa*]/
Aa or A as single matches (0 or more)
 
/[A-z]­{a,b}/
min a, max b
/[A-z]­{x,}/
min x times
/[A-z]{x}/
only x times
/[A-z]­,{x}/
max x times
 

Flags

/regex/flag
structure
 
i
ignores case
g
global, searches multiple times

Character Classes & Groups

/[a-z]/
a thru z
/[a-zA-Z]/
a thru z and A thru Z
/[^a]/
exclude a
 
/(\w)/
group
/(\w)\1/
reuses (\w)