Skip to content

Instantly share code, notes, and snippets.

@tylerzey
Last active August 5, 2020 13:53
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tylerzey/d220867bd3221f9756edda94797a64d0 to your computer and use it in GitHub Desktop.
Save tylerzey/d220867bd3221f9756edda94797a64d0 to your computer and use it in GitHub Desktop.
A List Of Helpful Regex Examples
let re;
//this looks for the string between the slashes. it'll match hello but not HeLlo.
re = /hello/;
//the lower case i means be case insensitive. this will match HellO.
re = /hello/i;
// explaination for common search characters
re = /^h/i; //the ^ means must start with this. if pos 0 is not the letter/number, then this fails. This matches "hello" but not "Why hello!"
re = /ld$/i; //$ means that the string must end with this character or number. this matches 'hello world' but not 'hello worlds'
re = /^hello$/i; //must start and end with hello. this matches 'hello' but not 'hello world'
re = /^hello$/ig; // the lower case g means global. It means it searches the entire string to find multiple matches.
re = /^h.llo$/i; //the "." is basically a wild card for one char. 'hallo' would match this search
re = /h.*llo/i; //* is wild card for many char. 'heeeeeeeeeeeello' would match this search. same with 'heasdfasdfasdfllo'
re = /gre?a?y/i; //the ? means an optional char. it can be either of the char behind a ? or empty
re = /gre?a?y\?/i; // the \ esc meta chars to treat as literals.
re = /gr[ae]y/i; // must be an e or a
re = /[^GF]ray/i; // will match anything EXCEPT GF
re = /[A-Z]ray/; // will match any upper case letter
re = /[a-z]ray/; // will match any lower case letter
re = /[A-Za-z]ray/; // will match any letter
re = /[A-Za-z0-9]ray/; // will match any letter or num
// Braces {} - Quantifiers
re = /He[a-z]{3}/i; // must occur exatctly {m} times. looks at prev char/regex
re = /He[a-z]{2,3}/i; // must occur between range. looks at prev char/regex
re = /He[a-z]{2,}/i; // must occur at least m times.
// Parentheses () - Grouping
re = /([0-9]x){3}/; // matches num then letter x 3x
re = /^([0-9]x){3}$/; // matches ONLY exactly num x 3x b/c of ending dollar sign
// shorthand chars
re = /\w/; //word char
re = /\w+/; //plus = one or more words
re = /\W/; //non-word char
re = /\d/; //match any digit
re = /\d+/; //match any digit 0 or more times
re = /\D+/; //match any non-digit
const str = 'Hello World! My Name Is Tyler.';
//const str = 'grey';
class Regex {
constructor(str, re) {
this.str = str;
this.re = re;
}
mytest() {
if (this.re.test(this.str)) {
return [this.re.exec(this.str), `${this.str} contains the regex search of: ${this.re.source}`];
} else {
return `No luck! Hopefully, ${this.str} should not match ${this.re.source}`;
}
}
}
const result = new Regex(str, re);
console.log(result.mytest());
@olarclara
Copy link

nice cheatsheet!

@Claire
Copy link

Claire commented Apr 30, 2018

clarification on line 16 https://gist.github.com/tylerzey/d220867bd3221f9756edda94797a64d0#file-regex-js-L16
the * says 0 or more of the previous letter or group so it would match abc123llo or 123llo456 anything as long as it has llo in it since h* doesn't have to occur (0 or more h's)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment