JavaScript Regular Expression Cheat Sheet

I have taken the following descriptions of regular expressions from FreeCodeCamp.com. Below is a high-level overview of these expressions.

/expression/ = expression starts and ends with /. Flags are appended
/g = find expression globally
/i = ignore case (a or A)
string.method(expression) = common regex structure
.test() = return a boolean if the expression is true
.match() = returns value from match
.replace() = (regularExp, string)
[abc] = character list (looks for either a,b,or c)
[-] = range of characters (a-z, 0-9)
[^abc] = do not look for these characters
+ = character repeats one after another
* = character repeats 0 or more times
^ = outside set, search from beginning
$ = search end of string
\w = [A-Za-z0-9_] \W = negate
\d = [0-9] \D = negate
\s = \s\t\f\n\v (string, tab, form feed, new line)
{lower, upper} = # of occurences, range or exact match
? = 0 or more optional
Positive lookahead: Make sure element in search pattern is there but won’t match the pattern.
(?=...) = … (expression) required expression but not matched.
Negative lookahead: Make sure element in search patter is not there
(!=...) = …(expression) pattern you don’t want
() = find repeated substrings \# = number of matches

Leave a Reply

Your email address will not be published. Required fields are marked *