Quick reference

The tiny regex cheatsheet.

Scan the building blocks, then open the playground to combine them.

$ token/hello/

looks for the string between the forward slashes (case-sensitive)

$ token/hello/i

looks for the string between the forward slashes (case-insensitive)

$ token/hello/g

looks for multiple occurrences of string between the forward slashes

$ token/h.llo/

the '.' matches any one character other than a new line character... matches 'hello', 'hallo' but not 'h
llo'

$ token/h.*llo/

the "*" matches any character(s) zero or more times... matches "hello", "heeeeeello", "hllo", "hwarwareallo"

$ token/\d/

matches any digit

$ token/\D/

matches any non-digit

$ token/\w/

matches any word character (a-z, A-Z, 0-9, _)

$ token/\W/

matches any non-word character

$ token/\s/

matches any white space character (\r (carriage return),\n (new line), \t (tab), \f (form feed))

$ token/\S/

matches any non-white space character

$ token/[abcd]/

matches any character in square brackets

$ token/[ch]at/

matches cat or hat

$ token/[^abcd]/

matches anything except the characters in square brackets

$ token/[a-z]/

matches all lowercase letters (a to z)

$ token/[A-Z]/

matches all uppercase letters (A to Z)

$ token/[0-9]/

matches all digits

$ token/[a-zA-Z]/

matches all lowercase and uppercase letters

$ token/[^a-zA-Z]/

matches non-letters

$ token/[a-zA-Z0-9]/

matches all lowercase, uppercase letters and numbers

$ token/(hello){4}/

matches "hellohellohellohello"

$ token/hello{3}/

matches "hellooo" and "helloooo" but not "helloo"

$ token/(hello){1,3}/

matches "hello" that occur between 1 and 3 times (inclusive)

$ token/(hello){3,}/

matches "hello" that occur atleast 3 times

$ token/ab*c/

matches zero or more repetitions of "b" (matches "abc", "abbbbc", "ac")

$ token/ab+c/

matches one or more repetitions of "b" (matches "abc", "abbbbc", but not "ac")

$ token/^/

matches beginning of a line

$ token/$/

matches end of a line

$ token/(hard)?work/

matches "work" or "hardwork"

$ token/(?:hard)?work/

matches "work" or "hardwork" but is a non-capturing group

$ token/i am a (cat|dog|whale) person/

matches "i am a cat person", "i am a dog person" and "i am a whale person"

$ token/z(?=a)/

positive lookahead... matches the "z" before the "a" in pizza but not the first "z"

$ token/z(?!a)/

negative lookahead... matches the first "z" but not the "z" before the "a"

$ token/(?<=[aeiou])\w/

positive lookbehind... matches any word character that is preceded by a vowel

$ token/(?<![aeiou])\w/

negative lookbehind... matches any word character that is not preceded by a vowel