Regex Basics Javascript Cheatsheet
May 09, 2020
Introduction
- Regex are the most commonly used ways for pattern matching, searching and replacing
- Below are some of the most commonly used regex concepts explained in simplest way possible.
- For more advanced expressions and explanation below are some links that might help.
Creating a REGEX expression:
Regex Constructor
let regex = new RegExp(pattern[, flags]);
Regex Literal ➡ Starts and Ends with /
let ex = /hello/
Flags:
i
➡ Case insensitiveg
➡ Global matchingm
➡ Multi-line search.s
➡ Allows . to match newline characters.u
➡ Unicodey
➡ Sticky search that matches starting at the current position in the target string.(useslastIndex
)Methods:
ex.exec('hellowolrd')
➡ Returns the index in array or nullconst myRe = /(?!a)\w/g; let myArray; while( (myArray = myRe.exec('appf apsnds aabb')) !== null){ console.log(myArray); } //Output [ 'dbbd', index: 1, input: 'cdbbddbbdsbz', groups: undefined ] [ 'dbbd', index: 5, input: 'cdbbddbbdsbz', groups: undefined ]
ex.test('phrase')
➡ Tests and returnstrue
orfalse
phrase.match(ex)
➡ Same likeexec
but returns the matches in array without looping forg
flags.const myRe = /db+d/g; let myArray myArray = 'cdbbddbbdsbz'.match(myRe) console.log(myArray) //Output [ 'dbbd', 'dbbd' ]
phrase.search(ex)
➡ Returns the index of the match else -1phrase.replace(ex, 'newPhrase')
➡ Returns string with replaced valueSymbols:
^
➡ Anything that starts with =>/^h/
$
➡ anything that ends with =>/world$/
.
➡ matches any one char*
➡ matches any 0 or more times?
➡ optional char =>/gre?a?y/
=> e or a or none+
➡ one or moreBrackets::
[]
➡ used for char set/gr[ea]y/
=> must be a or e[^]
➡ Used for exception/[^gf]ray/
=> Must not be g or f[a-zA-Z0-9]
➡ Used for range/[A-Z]ray/
=> anything in that range{}
➡ Quantifiers, Used to restrict the number of elements`/re{2}g/` ➡ 2 e's are allowed `/re{2,4}g/` ➡ 2 to 4 e's are allowed `/re{2,}g/` ➡ atleast 2 times e
()
➡ Used for grouping, also$1$2
(while replacing) or\1\2
can be used for specifying the groups in same statementShorthands:
\w
➡ word char or_
(underscore)\W
➡ non-word char\d
➡ any digit\D
➡ any non-digit\s
➡ any space\S
➡ any non space\b
➡ word boundary(only that word, matches the zero-width position)Lookaheads:
Positive -
/s(?=y)/
➡ s followed by y
Negative -/s(?!y)/
➡ s not followed by y
References:
Most used expressions are given above for deeper regex refer below links:-
Platforms for Practice:
Written by Prakadheshwaran Follow me on Twitter