Learn Regular Expressions

Learn Regular Expressions

    console.log("Hello from JavaScript");
    let sentence = "The dog chased the cat."
    let regex = /the/
    
    let myString = "Hello, World!";
    let myRegex = /Hello/;
    let result = myRegex.test(myString); 
    let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
    let waldoRegex = /Waldo/;
    let result = waldoRegex.test(waldoIsHiding);
    let petString = "James has a pet cat.";
    let petRegex = /dog|cat|bird|fish/;
    let result = petRegex.test(petString);
    let myString = "freeCodeCamp";
    let fccRegex = /freecodecamp/i; // Change this line
    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/g;
    testStr.match(ourRegex);
    
    let twinkleStar = "Twinkle, twinkle, little star";
    let starRegex = /twinkle/ig;
    let result = twinkleStar.match(starRegex); 
    
    console.log(result);
    let humStr = "I'll hum a song";
    let hugStr = "Bear hug";
    let huRegex = /hu./;
    humStr.match(huRegex); // Returns ["hum"]
    hugStr.match(huRegex); // Returns ["hug"]
    
    let exampleStr = "Let's have fun with regular expressions!";
    let unRegex = /.un/;
    let result = unRegex.test(exampleStr);
    
    console.log(result);
    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);
    let quoteSample = "The quick brown fox jumps over the lazy dog.";
    let alphabetRegex = /[a-z]/ig; 
    let result = quoteSample.malphabetRegex; 
    
    console.log(result);
    let quoteSample = "Blueberry 3.141592653s are delicious.";
    let myRegex = /[2-6h-s]/ig;
    let result = quoteSample.match(myRegex); 
    
    console.log(result);
    let quoteSample = "3 blind mice.";
    let myRegex = /change/; 
    let result = myRegex; 
    
    console.log(result);
    let difficultSpelling = "Mississipspi";
    let myRegex = /s+/g; 
    let result = difficultSpelling.match(myRegex);
    
    console.log(result);
    let soccerWord = "gooooooooal!";
    let gPhrase = "gut feeling";
    let oPhrase = "over the moon";
    let goRegex = /go*/;
    soccerWord.match(goRegex); // Returns ["goooooooo"]
    gPhrase.match(goRegex); // Returns ["g"]
    oPhrase.match(goRegex); // Returns null
    
    let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
    let chewieRegex = /Aa*/; // Change this line
    let result = chewieQuote.match(chewieRegex);
    
    console.log(result);
    let string = "titanic";
    let regex = /t[a-z]*?i/; 
    string.match(regex);
    
    let text = "<h1>Winter is coming</h1>";
    let myRegex = /<.*>/; 
    let result = text.match(myRegex);
    
    console.log(result);
    let crowd = 'P1P2P3P4P5P6CCCP7P8P9';
    
    let reCriminals = /C+/; // Change this line
    
    let matchedCriminals = crowd.match(reCriminals);
    console.log(matchedCriminals);
    let rickyAndCal = "Cal and Ricky both like racing.";
    let calRegex = /^/; // Change this line
    let result = calRegex.test(rickyAndCal);
    console.log(result);
    let caboose = "The last car on a caboose train is the ";
    let lastRegex = /caboose$/; // Change this line
    let result = lastRegex.test(caboose);
    console.log(result);
    let quoteSample = "The five boxing wizards jump quickly.";
    let alphabetRegexV2 = /\w/g; // Change this line
    let result = quoteSample.match(alphabetRegexV2).length;
    console.log(result);
    let quoteSample = "The five boxing wizards jump quickly.";
    let nonAlphabetRegex = /\W/; // Change this line
    let result = quoteSample.match(nonAlphabetRegex).length;
    console.log(result);
    let numString = "Your sandwich will be $5.00";
    let numRegex = /\d/g; // Change this line
    let result = numString.match(numRegex).length;
    console.log(result);
    let numString = "Your sandwich will be $5.00";
    let noNumRegex = /\d/g; // Change this line
    let result = numString.match(noNumRegex).length;
    console.log(result);
    /*
    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.
    */ 
    
    let username = "JackOfAllTrades";
    let userCheck = /^[A-Za-z]{2,}/; // Change this line
    let result = userCheck.test(username);
    let sample = "Whitespace is important in separating words";
    let countWhiteSpace = /\s/g; // Change this line
    let result = sample.match(countWhiteSpace);
    console.log(result);
    let sample = "Whitespace is important in separating words";
    let countWhiteSpace = /\s/g; // Change this line
    let result = sample.match(countWhiteSpace);
    console.log(result);
    let ohStr = "Ohhh no";
    let ohRegex = /Oh{3,6} no/; // Change this line
    let result = ohRegex.test(ohStr);
    let ohStr = "Ohhh no";
    let ohRegex = /Oh{3,6} no/; // Change this line
    let result = ohRegex.test(ohStr);
    let timStr = "Timmmmber";
    let timRegex = /Tim/; // Change this line
    let result = timRegex.test(timStr);
    let favWord = "favorite";
    let favRegex = /favou?rite/; // Change this line
    let result = favRegex.test(favWord);
    let quit = "qu";
    let noquit = "qt";
    let quRegex= /q(?=u)/;
    let qRegex = /q(?!u)/;
    quit.match(quRegex); // Returns ["q"]
    noquit.match(qRegex); // Returns ["q"]
    
    
    let sampleWord = "astronaut";
    let pwRegex = /(?=\w{5})/; // Change this line
    let result = pwRegex.test(sampleWord);
    let repeatStr = "regex regex";
    let repeatRegex = /(\w+)\s\1/;
    repeatRegex.test(repeatStr); // Returns true
    repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
    
    let repeatNum = "42 42 42 42";
    let reRegex = /(\d+)\s\1\s\1/; // Change this line
    let result = reRegex.test(repeatNum);
    let wrongText = "The sky is silver.";
    let silverRegex = /silver/;
    wrongText.replace(silverRegex, "blue");
    // Returns "The sky is blue."
    
    "Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
    // Returns "Camp Code"
    
    let huhText = "This sandwich is good.";
    let fixRegex = /change/; // Change this line
    let replaceText = ""; // Change this line
    let result = huhText.replace(fixRegex, replaceText);
    let hello = "   Hello, World!  ";
    let wsRegex = /^\s+|\s+$/g; // Change this line
    let result = hello; // Change this line
    console.log(result);
    console.log("Congratulations!!!");
    
    // JavaScript Projects: https://www.youtube.com/playlist?list=PLWKjh

Finally a simple and interactive way to learn regular expressions 🎉

This course features freeCodeCamp's Regular Expressions curriculum recorded as Scrimba screencasts. It introduces you to regular expressions (also called regex or regexp) through the lens of JavaScript. And as this is a Scrimba course, you'll be able to experiment with the expressions whenever you want. This is crucial when learning regex, as there are a lot of details to remember. Interacting with the code makes the knowledge stick, as opposed to simply watching normal video tutorials.

About freeCodeCamp

The lectures are recorded by Beau Carnes. Beau is the man behind some of the most popular courses at freeCodeCamp's YouTube channel, so you'll be in good hands. He explains concepts clearly and thoroughly, but without wasting your time.

Good luck, and happy coding!