REGEX CHEATSHEET
Part 1
REGEX
A regular expression, or 'regex', is used to
match parts of a string. Below is my cheat
sheet for creating regular expressions.
Testing a Regex:
Use the .test() method
let testString = "My test string";
let testRegex = /string/;
testRegex.test(testString); // true
CHEAT SHEET
A regular expression, or 'regex', is used to
match parts of a string. Below is my cheat
sheet for creating regular expressions.
TESTING MULTIPLE PATTERNS:
Using the OR operator ‘|’
const regex = /yes|no|maybe/;
IGNORING CASE:
Using the i flag for case insensitivity
const caseInsensitiveRegex = /ignore case/i;
const testString =
'We use the i flag to iGnOrE CasE';
caseInsensitiveRegex.test(testString); // true
MATCHING ANY CHARACTER:
Using the wildcard character . as a placeholder
// To match "cat", "BAT", "fAT", "mat"
const regexExpr = /.at/gi;
const testString = "cat BAT cupcake fAT mat dog";
const allMatchingWords =
testString.match(regexExpr);
// ["cat", "BAT", "fAT", "mat"]

Regex cheatsheet part-1

  • 1.
  • 2.
    REGEX A regular expression,or 'regex', is used to match parts of a string. Below is my cheat sheet for creating regular expressions. Testing a Regex: Use the .test() method let testString = "My test string"; let testRegex = /string/; testRegex.test(testString); // true
  • 3.
    CHEAT SHEET A regularexpression, or 'regex', is used to match parts of a string. Below is my cheat sheet for creating regular expressions. TESTING MULTIPLE PATTERNS: Using the OR operator ‘|’ const regex = /yes|no|maybe/;
  • 4.
    IGNORING CASE: Using thei flag for case insensitivity const caseInsensitiveRegex = /ignore case/i; const testString = 'We use the i flag to iGnOrE CasE'; caseInsensitiveRegex.test(testString); // true MATCHING ANY CHARACTER: Using the wildcard character . as a placeholder // To match "cat", "BAT", "fAT", "mat" const regexExpr = /.at/gi; const testString = "cat BAT cupcake fAT mat dog"; const allMatchingWords = testString.match(regexExpr); // ["cat", "BAT", "fAT", "mat"]