Skip to content

Instantly share code, notes, and snippets.

@jzevin
Created June 17, 2019 01:27
Show Gist options
  • Save jzevin/9318bda7df092a0ea166cc6d82a1f7c1 to your computer and use it in GitHub Desktop.
Save jzevin/9318bda7df092a0ea166cc6d82a1f7c1 to your computer and use it in GitHub Desktop.
let re = /hello/i; //insensitive
// Meta chars
re = /^h/; // Starts with lower case h => false
re = /^h/i; // Starts with any case h => true
re = /--$/i; // end with -- => true
re = /^--$/i; // must begin and end with -- => false
re = /w.rld/i; // matches any ONE char => true
re = /w*d/i; // matches any char 0 or more times => true
re = /w?rld/i; // matches optional character => true
re = /s*thing\?/i; // escape character matches literal '?' => true
//brackets - character sets
re = /w[0o]rld/; // matches either 0 or o characters between => true
re = /w[^0o]rld/; // matches anything but 0 or o characters => false
re = /w[A-F]rld/; // matches any uppercase letter => false
re = /w[a-f]rld/; // matches any lowercase letter => false
re = /w[A-Fa-f0-9]rld/; // matches any uppercase, lowercase, or number letter => true
//braces {} - Quantifiers
re = /wil{2}ows/; // char before must occur 2 times => true
re = /wil{2,4}ows/; // char before must occur 2-4 times => true
re = /wil{3,}ows/; // char before must occur at least 3 times => false
// Parenthesis () - Grouping
re = /([0-9][X-Z]){3}/; // matches number 0-9 followed by either X,Y, or Z 3 times => true
// Shorthand Character Classes
re = /\w/ // word character or _ => true
re = /\w+/ // one or more word characters => true
re = /\W/ // non word characters => true
re = /\d/ // digits => true
re = /\d+/ // digits on or more times => true
re = /\D/ // non digits anything but 0-9 => true
re = /\s/ // white space => true
re = /\S/ // non white space => true
re = /will\b/ // word boundary "will" => true
// Assertions
re = /o(?=ws)/ // Match "o" followed by a "ws" => true
re = /some(?!\s)/ // Match "some" NOT followed by whitespace => true
const str = 'Hello w0rld there willows 5X5Y5Z will is, **something?--';
has(re, str);
function has(regEx, str) {
console.log(regEx.test(str));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment