Part 1
1. Using the Test Method to Check Literal Strings
- The
.test()
method takes the regex, applies it to a string (which is placed inside the parentheses), and returns true or false if your pattern finds something or not. - Notice that quote marks are not required within the regular expression
- Case sensitive
let testRegex = /Code/;
let testStr = "freeCodeCamp";
testRegex.test(testStr);
// Returns true
2. Match a Literal String with Different Possibilities
- Search for multiple patterns using the
alternation
or OR operator:|
, for instance,/yes|no|maybe
let petString = "James has a pet cat.";
let petRegex = /dog|cat|bird|fish/;
let result = petRegex.test(petString);
3. Use flag i
to match both upper and lower cases
let myString = "Sunflower";
let fccRegex = /sunfower/i;
let result = fccRegex.test(myString);
result;//true
4. Extract Matches
- Use
.match()
method and apply the method on a string and pass in the regex inside the parentheses - The
.match
syntax is the “opposite” of the.test
method you have been using thus far
'string'.match(/regex/);
/regex/.test('string');
- Example 1
"Hello, World!".match(/Hello/);
// Returns ["Hello"]
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
// Returns ["expressions"]
- Example 2
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line
5. Use g
Flag to Find More Than the First Match
- Example
let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /Twinkle/gi;
let result = twinkleStar.match(starRegex);
6. Match Anything with Wildcard Period
- Sometimes you won’t (or don’t need to) know the exact characters in your patterns. Thinking of all words that match, say, a misspelling would take a long time. Luckily, you can save time using the wildcard character:
.
- The wildcard character
.
will match any one character. The wildcard is also calleddot
andperiod
. You can use the wildcard character just like any other character in the regex. - For example, if you wanted to match “hug”, “huh”, “hut”, and “hum”, you can use the regex
/hu./
to match all four words - Example 1
let humStr = "I'll hum a song";
let hugStr = "Bear hug";
let huRegex = /hu./;
huRegex.test(humStr); // Returns true
huRegex.test(hugStr); // Returns true
- Example 2
let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /.un/; // Change this line
let result = unRegex.test(exampleStr);
7. Use Character Sets to Match Single Character with Multiple Possibilities
- Search for a literal pattern with some flexibility with character classes, which allow you to define a group of characters you wish to match by placing them inside square brackets
[]
- Example 1
let bigStr = "big";
let bagStr = "bag";
let bugStr = "bug";
let bogStr = "bog";
let bgRegex = /b[aiu]g/;
bigStr.match(bgRegex); // Returns ["big"]
bagStr.match(bgRegex); // Returns ["bag"]
bugStr.match(bgRegex); // Returns ["bug"]
bogStr.match(bgRegex); // Returns null
- Example 2
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /[aeiou]/gi; // Change this line
let result = quoteSample.match(vowelRegex); // Change this line
8. Match Letters of the Alphabet
- Inside a character set, you can define a range of characters to match using
[]
and-
- Example 1
let catStr = "cat";
let batStr = "bat";
let matStr = "mat";
let bgRegex = /[a-e]at/;
catStr.match(bgRegex); // Returns ["cat"]
batStr.match(bgRegex); // Returns ["bat"]
matStr.match(bgRegex); // Returns null
- Example 2
let quoteSample = "The quick brown fox jumps over the lazy dog.";
let alphabetRegex = /[a-z]/ig;
let result = quoteSample.match(alphabetRegex);
9. Match Numbers and Letters of the Alphabet
/[0-5]/
matches any number between 0 and 5, including the 0 and 5- Combine a range of letters and numbers in a single character set
- Example 1
let jennyStr = "Jenny8675309";
let myRegex = /[a-z0-9]/ig;
// matches all letters and numbers in jennyStr
jennyStr.match(myRegex);
- Example 2
// 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.
let quoteSample = "Blueberry 3.141592653s are delicious.";
let myRegex = /[h-s2-6]/gi;
let result = quoteSample.match(myRegex);
10. Match Single Characters Not Specified
- Create a set of characters that you do not want to match. These types of character sets are called negated character sets.
- Create a negated character set, you place a caret character
^
after the opening bracket and before the characters you do not want to match. /[^aeiou]/gi
matches all characters that are not a vowel- Note that characters like
., !, [, @, /
and white space are matched - the negated vowel character set only excludes the vowel characters - Example
let quoteSample = "3 blind mice.";
let myRegex = /[^aeiou0-9]/gi; // Change this line
let result = quoteSample.match(myRegex); // Change this line
11. Match Characters that Occur One or More Times
-
You can use the
+
character to check if that is the case. Remember, the character or pattern has to be present consecutively. That is, the character has to repeat one after the other. -
Example 1
-
/a+/g
would find one match in"abc"
and return["a"]
. Because of the+
, it would also find a single match in"aabc"
and return["aa"]
. -
If it were instead checking the string
"abab"
, it would find two matches and return["a", "a"]
because thea
characters are not in a row - there is ab
between them.
-
// You want to find matches when the letter s occurs one or more times in "Mississippi". Write a regex that uses the + sign.
let difficultSpelling = "Mississippi";
let myRegex = /s+/g; // Change this line
let result = difficultSpelling.match(myRegex);
12. Match Characters that Occur Zero or More Times using *
- Example 1
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
- Example 2
// Create a regex chewieRegex that uses the * character to match an uppercase "A" character immediately followed by zero or more lowercase "a" characters in chewieQuote. Your regex does not need flags or character classes, and it should not match any of the other quotes.
let chewieRegex = /A0*a+/; // Change this line
let result = chewieQuote.match(chewieRegex);