Regular Expressions
let sentence = "The dog chased the cat."
let regex = /the/
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString);
Match a literal with different possibilities
let petString = "James has a pet cat.";
let petRegex = /dog|cat|bird|fish/;
let result = petRegex.test(petString);
Ignore Case
let myString = "freeCodeCamp";
let fccRegex = /freecodecamp/i;
let result = fccRegex.test(myString);
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/;
let result = extractStr.match(codingRegex);
console.log(result);
let testStr = "Repeat, Repeat, Repeat";
let ourRegex = /Repeat/;
testStr.match(ourRegex);
let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /twinkle/ig;
let result = twinkleStar.match(starRegex);
Match anything with wildcard period
let humStr = "I'll hum a song";
let hugStr = "Bear hug";
let huRegex = /hu./;
humStr.match(huRegex);
hugStr.match(huRegex);
let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /.un/;
let result = unRegex.test(exampleStr);
console.log(result);
Match single character with multiple possibilities
let bgRegex = /b[aiu]g/;
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /[aeiou]/ig;
let result = quoteSample.match(vowelRegex);
console.log(result);
Matching a range of letters
let quoteSample = "The quick brown fox jumps over the lazy dog.";
let alphabetRegex = /[a-z]/ig;
let result = quoteSample.match(alphabetRegex);
console.log(result);
Match Numbers & Letters
let quoteSample = "Blueberry 3.141592653s are delicious.";
let myRegex = /[2-6h-s]/ig;
let result = quoteSample.match(myRegex);
console.log(result);
Exclude (aka negate) numbers & characters
let quoteSample = "3 blind mice.";
let myRegex = /[^0-9aeiou]/ig;
let result = quoteSample.match(myRegex);
console.log(result);
Match characters that occur one or more time
let difficultSpelling = "Mississipspi";
let myRegex = /s+/g;
let result = difficultSpelling.match(myRegex);
console.log(result);
Match character that occur zero or more Times
let soccerWord = "gooooooooal!";
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
soccerWord.match(goRegex);
gPhrase.match(goRegex);
oPhrase.match(goRegex);
let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
let chewieRegex = /Aa*/;
let result = chewieQuote.match(chewieRegex);
console.log(result);
Lazy & Greedy Matches
let string = "titanic";
let regexGreedy = /t[a-z]*i/;
let regexLazy = /t[a-z]*?i/;
string.match(regex);
let text = "<h1>Winter is coming</h1>";
let myRegexGreedy = /<.*?>/;
let myRegexLazy = /<.*?>/;
let result = text.match(myRegex);
console.log(result);
Find one or more occurance of a character (say C)
let crowd = 'P1P2P3P4P5P6CCCP7P8P9';
let reCriminals = /C+/;
let matchedCriminals = crowd.match(reCriminals);
console.log(matchedCriminals);
Match beginning string pattern
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/;
let result = calRegex.test(rickyAndCal);
console.log(result);
Match ending string pattern
let caboose = "The last car on a train is the caboose";
let lastRegex = /caboose$/;
let result = lastRegex.test(caboose);
console.log(result);
Short hand classes:
Match all letters & numbers (also includes underscore)
let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /\\w/g;
let result = quoteSample.match(alphabetRegexV2).length;
console.log(result);
Match everthing but letters & numbers
let quoteSample = "The five boxing wizards jump quickly.";
let nonAlphabetRegex = /\\W/g;
let result = quoteSample.match(nonAlphabetRegex).length;
console.log(result);
Match all numbers
let numString = "Your sandwich will be $5.00";
let numRegex = /\\d/g;
let result = numString.match(numRegex).length;
console.log(result);
Match all non-numbers
let numString = "Your sandwich will be $5.00";
let noNumRegex = /\\D/g;
let result = numString.match(noNumRegex).length;
console.log(result);
Restrict possible username
let username = "JackOfAllTrades";
let userCheck = /^[A-Za-z]{2,}\\d*$/;
let result = userCheck.test(username);
Match whitespace characters
let sample = "Whitespace is important in separating words";
let countWhiteSpace = /\\s/g;
let result = sample.match(countWhiteSpace);
console.log(result);
Match non-whitespace characters
let sample = "Whitespace is important in separating words";
let countWhiteSpace = /\\S/g;
let result = sample.match(countWhiteSpace);
console.log(result);
References: