Regular Expressions

In Cheat Sheets by Hoani Bryson

// Literal characters
re = /hello/;
re = /hello/i;

// Metacharacter Symbols
re = /^h/i;         // ^  - Must start with
re = /orld$/i;      // $  - Must end with
re = /^hello$/i;    // ^$ - Must start and end with
re = /h.llo/i;      // .  - Matches any ONE character
re = /h*w/i;        // *  - Matches any character zero or more times
re = /gre?a?y/i;    // ?  - optional character, can exist or not 
                    //      (valid inputs are Gry, Grey, Gray, Greay)
re = /gre?a?y\?/i;  // \  - escape characters

// Brackets [] - Character sets
re = /gr[ae]y/i;    // must be an a or e only
re = /[GF]ray/;     // must be a G or F only
re = /[^GF]ray/;    // Match anything except G or F (see ^ as NOT)
re = /[A-Z]ray/;    // Match any uppercase letter
re = /[a-z]ray/;    // Match any lowercase letter
re = /[A-Za-z]ray/; // Match any letter
re = /[0-9]ray/;    // Match any digit

// Braces {} - Quantifiers
re = /Hel{2}o/i;    // Match exactly {N} times 
re = /Hel{2,4}o/i;  // Match within the range times 
re = /Hel{2,}o/i;   // Match at least {N} times 

// Parentheses () - Grouping
re = /([0-9]x){3}$/;


// Shorthand character classes
re = /\w/;          // \w - Word character (alphanumeric - any char, number or _)
re = /^\w+$/;       // Match word characters only
re = /\W/;          // \W - Non-word character
re = /\d/;          // \d - Any digit
re = /\d+/;         // \d - Any digit multiple times
re = /\D/;          // \d - Any non-digit
re = /\s/;          // \s - Any whitespace character
re = /\S/;          // \S - Any non-whitespace character
re = /Hell\b/i;     // \b - Word boundary - word must end at \b

// Assertions
re = /x(?=y)/;      // Match x only if it is followed by y
re = /x(?!y)/;      // Match x only if it is not followed by y