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