d:\Electronics\web\regex_learning\ex1\ex1.
js Monday, April 26, 2021 11:50 PM
// Begin learning Regular Expression
console.log("Hello world");
let petString = "James has a cat.";
let petRegex = /cat/;
let result = petRegex.test(petString);
console.log(result);
// Ignore Case while Matching
let myString = "freeCodeCamp";
let fccRegex = /freecodecamp/i;
result = fccRegex.test(myString);
console.log(result);
let extractStr = "Extract the world 'coding' from this string";
let codingRegex = /coding/;
result = extractStr.match(codingRegex);
console.log(result);
// Find more than the first match
let testStr = "Repeat, Repeat, Repeat";
let ourRegex = /Repeat/g;
result = testStr.match(ourRegex);
console.log(result);
let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /twinkle/ig;
result = twinkleStar.match(starRegex);
console.log(result);
// Match anything with Wildcard period
let humStr = "I'll hum a song";
let hugStr = "Bear hug";
let huRegex = /hu./;
console.log(humStr.match(huRegex));
console.log(hugStr.match(huRegex));
let exampleStr = "Let's have fun with regular expression!";
let unRegex = /.un/;
console.log(unRegex.test(exampleStr));
console.log(exampleStr.match(unRegex));
// Match single character with multiple possiblilities
bgRegex = /b[aiu]g/;
let quoteSample = "Beware of bugs in the above code; I have only proved it correct";
let vowelRegex = /[aeiou]/ig;
console.log(quoteSample.match(vowelRegex));
console.log(quoteSample.match(bgRegex));
// Match Letters of the Alphabet
let quoteSample1 = "The quick brown for jumps over the lazy dog.";
let alphabetRegex = /[a-z]/ig;
console.log(quoteSample1.match(alphabetRegex));
// Match number and letters of the Alphabet
let quoteSample2 = "Blueberry 3.141592653s are delicious.";
let alphabetRegex2 = /[2-6h-s]/ig;
console.log(quoteSample2.match(alphabetRegex2));
// Match single characters not specified
let quoteSample3 = "3 blink mice.";
let myRegex = /[^0-9aeiou]/ig;
console.log(quoteSample3.match(myRegex));
// Match characters that occur one or more times
let difficultSpelling = "Mississipspi";
myRegex = /s+/g;
console.log(difficultSpelling.match(myRegex));
// Match characters that occur zero or more times
let soccerWord = "gooooooooal!";
-1-
d:\Electronics\web\regex_learning\ex1\ex1.js Monday, April 26, 2021 11:50 PM
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
console.log(soccerWord.match(goRegex));
console.log(gPhrase.match(goRegex));
console.log(oPhrase.match(goRegex));
let chewieQuote = "Aaaaaaaaaaaaaaaaaaaaaaaaarrgh!";
let chewieRegex = /Aa*/;
console.log(chewieQuote.match(chewieRegex));
// Find characters with lazy matching
let string = "titanic";
let regex = /t[a-z]*i/;
console.log(string.match(regex));
regex = /t[a-z]*?i/; //it calls lazy match with '?'
console.log(string.match(regex));
let text = "<h1>Winter is coming</h1>";
let myRegex2 = /<.*>/;
console.log(text.match(myRegex2));
myRegex2 = /<.*?>/; // it calls lazy match with inserting '?' sign
console.log(text.match(myRegex2));
// Find one or more criminals in a Hunt
let crowd = 'P1P2P3P4P5P6CCCP7P8P9';
let reCriminals = /C+/;
let matchedCriminals = crowd.match(reCriminals);
console.log(matchedCriminals);
// Match beginning string patterns
let rickyAndCal = "Cal and Ricky both like racing";
let calRegex = /^Cal/;
console.log(calRegex.test(rickyAndCal));
// Matching ending string patterns
let caboose = "The last car on a train is the caboose";
let lastRegex = /caboose$/;
console.log(lastRegex.test(caboose));
// Match all letters and numbers
quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /\w/g;
result = quoteSample.match(alphabetRegexV2).length;
console.log(result);
// Match everything but letters and numbers
quoteSample = "The five boxing wizards jump quickly.";
let nonAlphabetRegex = /\W/g;
result = quoteSample.match(nonAlphabetRegex).length;
console.log(result);
// Match all numbers
let numString = "Your sandwich will be $5.00";
let numRegex = /\d/g;
result = numString.match(numRegex).length;
console.log(result);
// Match all non-numbers
numString = "Your sandwich will be $5.00";
let noNumRegex = /\D/g;
result = numString.match(noNumRegex).length;
console.log(result);
// Restrict possible usernames
/*
1) If there are numbers, they must be at the end.
2) Letters can be lowercase and uppercase.
3) At least two characters long. Two-letter names can't have numbers.
*/
-2-
d:\Electronics\web\regex_learning\ex1\ex1.js Monday, April 26, 2021 11:50 PM
let username = "JackOfAllTrades";
let userCheck = /^[A-Za-z]{2,}\d*$/;
result = userCheck.test(username);
console.log(result);
// Mach whitespace
let sample = "Whitespace is important in separating words";
let countWhiteSpace = /\s/g;
result = sample.match(countWhiteSpace);
console.log(result);
// Match non-whitespace characters
sample = "Whitespace is important in separating words";
countWhiteSpace = /\S/g;
result = sample.match(countWhiteSpace);
console.log(result);
// Specify upper and lower number of matches
let ohStr = "Ohhh no";
let ohRegex = /Oh{3,6} no/;
result = ohRegex.test(ohStr);
console.log(result);
// Specify only the lower number of matches
let haStr = "Hazzzzah";
let haRegex = /z{4,}/;
result = haRegex.test(haStr);
console.log(result);
console.log(haStr.match(haRegex));
// Specify exact number of matches
let timStr = "Timmmmber";
let timRegex = /Tim{4}ber/;
result = timRegex.test(timStr);
console.log(result);
console.log(timStr.match(timRegex));
// Check for all or none
let favWord = "favorite";
let favRegex = /favou?rite/;
result = favRegex.test(favWord);
console.log(result);
console.log(favWord.match(favRegex));
// Positive and negative lookahead
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/; // positive lookahead
let qRegex = /q(?!u)/; // negative lookahead
console.log(quit.match(quRegex));
console.log(noquit.match(qRegex));
let sampleWord = "astronaut362";
let pwRegex = /(?=\w{5})(?=\D*\d{2})/; // it means matching five or more characters and two
or more digits, it usually use for check typing password. "D*" in above Regex mean from
beginning of sampleWord not contains any digits, if contains digits is at least 2 digits
(d{2})
result = pwRegex.test(sampleWord);
console.log(result);
console.log(sampleWord.match(pwRegex));
// Reuse patterns using capture groups
let repeatStr = "regex regex";
let repeatRegex = /(\w+)\s\1/; // the parenthesis () is represent for capture groups
console.log(repeatRegex.test(repeatStr));
console.log(repeatStr.match(repeatRegex));
let repeatNum = "42 42 42";
let reRegex = /(\d+)\s\1\s\1/;
let reRegex2 = /^(\d+)\s\1\s\1$/;
-3-
d:\Electronics\web\regex_learning\ex1\ex1.js Monday, April 26, 2021 11:50 PM
result = reRegex.test(repeatNum);
console.log(result);
console.log(repeatNum.match(reRegex));
console.log(reRegex2.test(repeatNum));
console.log(repeatNum.match(reRegex2));
// Use capture groups to search and replace
let wrongText = "The sky is silver";
let silverRegex = /silver/;
console.log(wrongText.replace(silverRegex, "blue"));
let codeCampStr = "Code Camp";
//result = "Code Camp".replace(/(w+)\s(w+)/, '$2 $1');
result = codeCampStr.replace(/(w+)\s(w+)/, '$2 $1');
console.log(result);
let huhText = "This sandwich is good.";
let fixRegex = /good/;
let replaceText = "okey-dokey";
result = huhText.replace(fixRegex, replaceText);
console.log(result);
// Remove whitespace from start and end
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
result = hello.replace(wsRegex, '');
console.log(result);
-4-