Skip to content

Commit 8d6e9c0

Browse files
committed
Regular Expressions (ex. 1 to 33)
+ Finished Regular Expressions module
1 parent 4c4e3b2 commit 8d6e9c0

35 files changed

+586
-34
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Using the Test Method:
3+
Apply the regex myRegex on the string myString using the .test() method.
4+
5+
- You should use .test() to test the regex.
6+
- Your result should return true.
7+
*/
8+
let myString = "Hello, World!";
9+
let myRegex = /Hello/;
10+
let result = myRegex.test(myString); // Changed this line
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Match Literal Strings:
3+
Complete the regex waldoRegex to find "Waldo" in the string waldoIsHiding with a literal match.
4+
5+
- Your regex waldoRegex should find the string Waldo
6+
- Your regex waldoRegex should not search for anything else.
7+
- You should perform a literal string match with your regex.
8+
*/
9+
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
10+
let waldoRegex = /Waldo/; // Changed this line
11+
let result = waldoRegex.test(waldoIsHiding);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Match a Literal String with Different Possibilities:
3+
Complete the regex petRegex to match the pets dog, cat, bird, or fish.
4+
5+
- Your regex petRegex should return true for the string John has a pet dog.
6+
- Your regex petRegex should return false for the string Emma has a pet rock.
7+
- Your regex petRegex should return true for the string Emma has a pet bird.
8+
- Your regex petRegex should return true for the string Liz has a pet cat.
9+
- Your regex petRegex should return false for the string Kara has a pet dolphin.
10+
- Your regex petRegex should return true for the string Alice has a pet fish.
11+
- Your regex petRegex should return false for the string Jimmy has a pet computer.
12+
*/
13+
let petString = "James has a pet cat.";
14+
let petRegex = /dog|cat|bird|fish/; // Changed this line
15+
let result = petRegex.test(petString);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Ignore Case While Matching:
3+
Write a regex fccRegex to match freeCodeCamp, no matter its case. Your regex should not match any abbreviations or variations with spaces.
4+
5+
- Your regex should match the string freeCodeCamp
6+
- Your regex should match the string FreeCodeCamp
7+
- Your regex should match the string FreecodeCamp
8+
- Your regex should match the string FreeCodecamp
9+
- Your regex should not match the string Free Code Camp
10+
- Your regex should match the string FreeCOdeCamp
11+
- Your regex should not match the string FCC
12+
- Your regex should match the string FrEeCoDeCamp
13+
- Your regex should match the string FrEeCodECamp
14+
- Your regex should match the string FReeCodeCAmp
15+
*/
16+
let myString = "freeCodeCamp";
17+
let fccRegex = /freeCodeCamp/i; // Changed this line
18+
let result = fccRegex.test(myString);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Extract Matches:
3+
Apply the .match() method to extract the string coding.
4+
5+
- The result should have the string coding
6+
- Your regex codingRegex should search for the string coding
7+
- You should use the .match() method.
8+
*/
9+
let extractStr = "Extract the word 'coding' from this string.";
10+
let codingRegex = /coding/; // Changed this line
11+
let result = extractStr.match(codingRegex); // Changed this line
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Find More Than the First Match:
3+
Using the regex starRegex, find and extract both Twinkle words from the string twinkleStar.
4+
5+
Note: You can have multiple flags on your regex like /search/gi
6+
7+
- Your regex starRegex should use the global flag g
8+
- Your regex starRegex should use the case-insensitive flag i
9+
- Your match should match both occurrences of the word Twinkle
10+
- Your match result should have two elements in it.
11+
*/
12+
let twinkleStar = "Twinkle, twinkle, little star";
13+
let starRegex = /twinkle/gi; // Changed this line
14+
let result = twinkleStar.match(starRegex); // Changed this line
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Match Anything with Wildcard Period:
3+
Complete the regex unRegex so that it matches the strings run, sun, fun, pun, nun, and bun. Your regex should use the wildcard character.
4+
5+
- You should use the .test() method.
6+
- You should use the wildcard character in your regex unRegex
7+
- Your regex unRegex should match run in the string Let us go on a run.
8+
- Your regex unRegex should match sun in the string The sun is out today.
9+
- Your regex unRegex should match fun in the string Coding is a lot of fun.
10+
- Your regex unRegex should match pun in the string Seven days without a pun makes one weak.
11+
- Your regex unRegex should match nun in the string One takes a vow to be a nun.
12+
- Your regex unRegex should match bun in the string She got fired from the hot dog stand for putting her hair in a bun.
13+
- Your regex unRegex should not match the string There is a bug in my code.
14+
- Your regex unRegex should not match the string Catch me if you can.
15+
*/
16+
let exampleStr = "Let's have fun with regular expressions!";
17+
let unRegex = /.un/; // Changed this line
18+
let result = unRegex.test(exampleStr);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Match Single Character with Multiple Possibilities:
3+
Use a character class with vowels (a, e, i, o, u) in your regex vowelRegex to find all the vowels in the string quoteSample.
4+
5+
Note: Be sure to match both upper- and lowercase vowels.
6+
7+
- You should find all 25 vowels.
8+
- Your regex vowelRegex should use a character class.
9+
- Your regex vowelRegex should use the global flag.
10+
- Your regex vowelRegex should use the case-insensitive flag.
11+
- Your regex should not match any consonants.
12+
*/
13+
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
14+
let vowelRegex = /[aeiou]/gi; // Changed this line
15+
let result = quoteSample.match(vowelRegex); // Changed this line
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Match Letters of the Alphabet:
3+
Match all the letters in the string quoteSample.
4+
5+
Note: Be sure to match both uppercase and lowercase letters.
6+
7+
- Your regex alphabetRegex should match 35 items.
8+
- Your regex alphabetRegex should use the global flag.
9+
- Your regex alphabetRegex should use the case-insensitive flag.
10+
*/
11+
let quoteSample = "The quick brown fox jumps over the lazy dog.";
12+
let alphabetRegex = /[a-z]/gi; // Changed this line
13+
let result = quoteSample.match(alphabetRegex); // Changed this line
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Match Numbers and Letters of the Alphabet:
3+
Create a single regex that matches a range of letters between h and s, and a range of numbers between 2 and 6. Remember to include the appropriate flags in the regex.
4+
5+
- Your regex myRegex should match 17 items.
6+
- Your regex myRegex should use the global flag.
7+
- Your regex myRegex should use the case-insensitive flag.
8+
*/
9+
let quoteSample = "Blueberry 3.141592653s are delicious.";
10+
let myRegex = /[h-s2-6]/gi; // Changed this line
11+
let result = quoteSample.match(myRegex); // Changed this line
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Match Single Characters Not Specified:
3+
Create a single regex that matches all characters that are not a number or a vowel. Remember to include the appropriate flags in the regex.
4+
5+
- Your regex myRegex should match 9 items.
6+
- Your regex myRegex should use the global flag.
7+
- Your regex myRegex should use the case-insensitive flag.
8+
*/
9+
let quoteSample = "3 blind mice.";
10+
let myRegex = /[^aeiou0-9]/gi; // Changed this line
11+
let result = quoteSample.match(myRegex); // Changed this line
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Match Characters that Occur One or More Times:
3+
You want to find matches when the letter s occurs one or more times in Mississippi. Write a regex that uses the + sign.
4+
5+
- Your regex myRegex should use the + sign to match one or more s characters.
6+
- Your regex myRegex should match 2 items.
7+
- The result variable should be an array with two matches of ss
8+
*/
9+
let difficultSpelling = "Mississippi";
10+
let myRegex = /s+/g; // Changed this line
11+
let result = difficultSpelling.match(myRegex);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Match Characters that Occur Zero or More Times:
3+
For this challenge, chewieQuote has been initialized as the string Aaaaaaaaaaaaaaaarrrgh! behind the scenes.
4+
Create a regex chewieRegex that uses the * character to match an uppercase A character immediately followed by zero or
5+
more lowercase a characters in chewieQuote. Your regex does not need flags or character classes, and it should not match
6+
any of the other quotes.
7+
8+
- Your regex chewieRegex should use the * character to match zero or more a characters.
9+
- Your regex should match the string A in chewieQuote.
10+
- Your regex should match the string Aaaaaaaaaaaaaaaa in chewieQuote.
11+
- Your regex chewieRegex should match 16 characters in chewieQuote.
12+
- Your regex should not match any characters in the string He made a fair move. Screaming about it can't help you.
13+
- Your regex should not match any characters in the string Let him have it. It's not wise to upset a Wookiee.
14+
*/
15+
const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
16+
let chewieRegex = /Aa*/; // Changed this line
17+
let result = chewieQuote.match(chewieRegex);
18+
19+
console.log(`Main result: ${result}`);
20+
let firstTest = "He made a fair move. Screaming about it can't help you";
21+
console.log(`1st test: ${firstTest.match(chewieRegex)}`);
22+
let secondTest = "Let him have it. It's not wise to upset a Wookiee";
23+
console.log(`2nd test: ${secondTest.match(chewieRegex)}`);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Find Characters with Lazy Matching:
3+
Fix the regex /<.*>/ to return the HTML tag <h1> and not the text "<h1>Winter is coming</h1>".
4+
Remember the wildcard . in a regular expression matches any character.
5+
6+
- The result variable should be an array with <h1> in it
7+
- myRegex should use lazy matching
8+
- myRegex should not include the string h1
9+
*/
10+
let text = "<h1>Winter is coming</h1>";
11+
let myRegex = /<.*?>/; // Changed this line
12+
let result = text.match(myRegex);
13+
14+
console.log(`Main result: ${result}`);
15+
16+
console.log(`1st test: ${"<h2>1st test</h2>".match(myRegex)}`);
17+
console.log(`2st test: ${"<b>2nd test</b>".match(myRegex)}`);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Find One or More Criminals in a Hunt:
3+
Write a greedy regex that finds one or more criminals within a group of other people. A criminal is represented by the capital letter C.
4+
5+
- Your regex should match one criminal (C) in the string C
6+
- Your regex should match two criminals (CC) in the string CC
7+
- Your regex should match three criminals (CCC) in the string P1P5P4CCCcP2P6P3.
8+
- Your regex should match five criminals (CCCCC) in the string P6P2P7P4P5CCCCCP3P1
9+
- Your regex should not match any criminals in the empty string ""
10+
- Your regex should not match any criminals in the string P1P2P3
11+
- Your regex should match fifty criminals (CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC) in the string P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3.
12+
*/
13+
let reCriminals = /C+/; // Changed this line
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Match Beginning String Patterns:
3+
Use the caret character in a regex to find Cal only in the beginning of the string rickyAndCal.
4+
5+
- Your regex should search for the string Cal with a capital letter.
6+
- Your regex should not use any flags.
7+
- Your regex should match the string Cal at the beginning of the string.
8+
- Your regex should not match the string Cal in the middle of a string.
9+
*/
10+
let rickyAndCal = "Cal and Ricky both like racing.";
11+
let calRegex = /^Cal/; // changed this line
12+
let result = calRegex.test(rickyAndCal);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Match Ending String Patterns:
3+
Use the anchor character ($) to match the string caboose at the end of the string caboose.
4+
5+
- You should search for caboose with the dollar sign $ anchor in your regex.
6+
- Your regex should not use any flags.
7+
- You should match caboose at the end of the string The last car on a train is the caboose
8+
*/
9+
let caboose = "The last car on a train is the caboose";
10+
let lastRegex = /caboose$/; // Changed this line
11+
let result = lastRegex.test(caboose);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Match All Letters and Numbers:
3+
Use the shorthand character class \w to count the number of alphanumeric characters in various quotes and strings.
4+
5+
- Your regex should use the global flag.
6+
- Your regex should use the shorthand character \w to match all characters which are alphanumeric.
7+
- Your regex should find 31 alphanumeric characters in the string The five boxing wizards jump quickly.
8+
- Your regex should find 32 alphanumeric characters in the string Pack my box with five dozen liquor jugs.
9+
- Your regex should find 30 alphanumeric characters in the string How vexingly quick daft zebras jump!
10+
- Your regex should find 36 alphanumeric characters in the string 123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.
11+
*/
12+
let quoteSample = "The five boxing wizards jump quickly.";
13+
let alphabetRegexV2 = /\w/g; // Changed this line
14+
let result = quoteSample.match(alphabetRegexV2).length;
15+
16+
console.log(`Main result expects 31 and returns: ${result}`);
17+
const firstTest = "Pack my box with five dozen liquor jugs.";
18+
console.log(`1st test expects 32 and returns: ${firstTest.match(alphabetRegexV2).length}`);
19+
const secondTest = "How vexingly quick daft zebras jump!";
20+
console.log(`2nd test expects 30 and returns: ${secondTest.match(alphabetRegexV2).length}`);
21+
const thirdTest = "123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.";
22+
console.log(`3rd test expects 36 and returns: ${thirdTest.match(alphabetRegexV2).length}`);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Match Everything But Letters and Numbers:
3+
Use the shorthand character class \W to count the number of non-alphanumeric characters in various quotes and strings.
4+
5+
- Your regex should use the global flag.
6+
- Your regex should find 6 non-alphanumeric characters in the string The five boxing wizards jump quickly.
7+
- Your regex should use the shorthand character to match characters which are non-alphanumeric.
8+
- Your regex should find 8 non-alphanumeric characters in the string Pack my box with five dozen liquor jugs.
9+
- Your regex should find 6 non-alphanumeric characters in the string How vexingly quick daft zebras jump!
10+
- Your regex should find 12 non-alphanumeric characters in the string 123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.
11+
*/
12+
let quoteSample = "The five boxing wizards jump quickly.";
13+
let nonAlphabetRegex = /\W/g; // Changed this line
14+
let result = quoteSample.match(nonAlphabetRegex).length;
15+
16+
console.log(`Main result expects 6 and returns: ${result}`);
17+
const firstTest = "Pack my box with five dozen liquor jugs.";
18+
console.log(`1st test expects 8 and returns: ${firstTest.match(nonAlphabetRegex).length}`);
19+
const secondTest = "How vexingly quick daft zebras jump!";
20+
console.log(`2nd test expects 6 and returns: ${secondTest.match(nonAlphabetRegex).length}`);
21+
const thirdTest = "123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.";
22+
console.log(`3rd test expects 12 and returns: ${thirdTest.match(nonAlphabetRegex).length}`);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Match All Numbers:
3+
Use the shorthand character class \d to count how many digits are in movie titles. Written out numbers ("six" instead of 6) do not count.
4+
5+
- Your regex should use the shortcut character to match digit characters
6+
- Your regex should use the global flag.
7+
- Your regex should find 1 digit in the string 9.
8+
- Your regex should find 2 digits in the string Catch 22.
9+
- Your regex should find 3 digits in the string 101 Dalmatians.
10+
- Your regex should find no digits in the string One, Two, Three.
11+
- Your regex should find 2 digits in the string 21 Jump Street.
12+
- Your regex should find 4 digits in the string 2001: A Space Odyssey.
13+
*/
14+
let movieName = "2001: A Space Odyssey";
15+
let numRegex = /\d/g; // Changed this line
16+
let result = movieName.match(numRegex).length;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Match All Non-Numbers:
3+
Use the shorthand character class for non-digits \D to count how many non-digits are in movie titles.
4+
5+
- Your regex should use the shortcut character to match non-digit characters
6+
- Your regex should use the global flag.
7+
- Your regex should find no non-digits in the string 9.
8+
- Your regex should find 6 non-digits in the string Catch 22.
9+
- Your regex should find 11 non-digits in the string 101 Dalmatians.
10+
- Your regex should find 15 non-digits in the string One, Two, Three.
11+
- Your regex should find 12 non-digits in the string 21 Jump Street.
12+
- Your regex should find 17 non-digits in the string 2001: A Space Odyssey.
13+
*/
14+
let movieName = "2001: A Space Odyssey";
15+
let noNumRegex = /\D/g; // Changed this line
16+
let result = movieName.match(noNumRegex).length;

0 commit comments

Comments
 (0)