Part 3
1. Specify Upper and Lower Number of Matches
- You can specify the lower and upper number of patterns with quantity specifiers. Quantity specifiers are used with curly brackets
{ }
. You put two numbers between the curly brackets - for the lower and upper number of patterns. - Example 1
// Match only the letter a appearing between 3 and 5 times in the string "eh", your regex would be `/a{3,5}h/`
let A4 = "eeeh";
let A2 = "eeh";
let multipleA = /e{3,5}h/;
multipleA.test(A4); // Returns true
multipleA.test(A2); // Returns false
- Example 2
// Change the regex ohRegex to match the entire phrase "Oh no" only when it has 3 to 6 letter h's
let ohStr = "Ohhh no";
let ohRegex = /Oh{3,6} no/;
let result = ohRegex.test(ohStr);
2. Specify Only the Lower Number of Matches
- You can specify the lower and upper number of patterns with quantity specifiers using curly brackets. Sometimes you only want to specify the lower number of patterns with no upper limit
- To only specify the lower number of patterns, keep the first number followed by a comma
- Example 1
// For example, to match only the string "hah" with the letter a appearing at least 3 times, your regex would be /ha{3,}h/.
let A4 = "haaaah";
let A2 = "haah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleA = /ha{3,}h/;
multipleA.test(A4); // Returns true
multipleA.test(A2); // Returns false
multipleA.test(A100); // Returns true
- Example 2
// Change the regex haRegex to match the word "Hazzah" only when it has four or more letter z's.
let haStr = "Hazzzzah";
let haRegex = /Haz{4,}ah/; // Change this line
let result = haRegex.test(haStr);
3. Specify Exact Number of Matches
- You can specify the lower and upper number of patterns with quantity specifiers using curly brackets. Sometimes you only want a specific number of matches.
- To specify a certain number of patterns, just have that one number between the curly brackets.
- Example 1
// Match only the word "hah" with the letter a 3 times, your regex would be /ha{3}h/.
let A4 = "haaaah";
let A3 = "haaah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleHA = /ha{3}h/;
multipleHA.test(A4); // Returns false
multipleHA.test(A3); // Returns true
multipleHA.test(A100); // Returns false
- Example 2
// Change the regex timRegex to match the word "Timber" only when it has four letter m's.
let timStr = "Timmmmber";
let timRegex = /Tim{4}ber/;
let result = timRegex.test(timStr);
4. Check for All or None
- Sometimes the patterns you want to search for may have parts of it that may or may not exist. However, it may be important to check for them nonetheless
- You can specify the
possible existence of an element
with a question mark,?
. This checks forzero or one of the preceding element
. You can think of this symbol assaying the previous element is optional
- Example 1:
// Use the question mark to match American and British English spellings
let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american);
rainbowRegex.test(british);
- Example 2
// Change the regex favRegex to match both the American English (favorite) and the British English (favourite) version of the word.
let favWord = "favorite";
let favRegex = /favou?rite/; // Change this line
let result = favRegex.test(favWord);
5. Positive and Negative Lookahead 🔭
-
Lookaheads are patterns that tell JavaScript to look-ahead in your string to check for patterns further along
-
This can be useful when you want to search for multiple patterns over the same string
-
There are two kinds of lookaheads:
- positive lookahead
- A positive lookahead will look to make sure the element in the search pattern is there, but won’t actually match it
- A positive lookahead is used as
?=...
where the...
is the required part that is not matched
- negative lookahead
- A negative lookahead will look to make sure the element in the search pattern is not there
- A negative lookahead is used as
?!...
where the...
is the pattern that you do not want to be there. - The rest of the pattern is returned if the negative lookahead part is not present
- positive lookahead
-
Example 1
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["q"]
//
quit.match(qRegex) // Returns null
noquit.match(quRegex) // Returns null
- Example 2
// Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits
let sampleWord = "astronaut";
let pwRegex = /^\D(?=\w{5})(?=\w*\d{2})/
// Change this line
let result = pwRegex.test(sampleWord);
6. Check For Mixed Grouping of Characters 🔖
-
Check for groups of characters using a Regular Expression and to achieve that we use parentheses ()
-
Example 1 🐧 🐧
//Find either Penguin or Pumpkin in a string
/P(engu|umpk)in/g
- Example 2
// Fix the regex so that it checks for the names of Franklin Roosevelt or Eleanor Roosevelt in a case sensitive manner and it should make concessions for middle names.
let myString = "Eleanor Roosevelt";
let myRegex = /(Eleanor|Franklin).* Roosevelt/; // Change this line
let result = myRegex.test(myString);
7. Reuse Patterns Using Capture Groups
- Some patterns you search for will occur multiple times in a string. It is wasteful to manually repeat that regex. There is a better way to specify when you have multiple repeat substrings in your string.
- You can search for repeat substrings using
capture groups
–( repeat pattern )
- To specify where that repeat string will appear, you use a backslash
\
and thena number
. This number starts at 1 and increases with each additional capture group you use. An example would be\1
to match the first group. - Using the
.match()
method on a string willreturn an array with the string it matches, along with its capture group
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";
let reRegex = /^(\d+)\s\1\s\1$/; // Change this line
let result = reRegex.test(repeatNum);
8. Use Capture Groups to Search and Replace
- You can search and replace text in a string using
.replace()
on a string. The inputs for.replace()
is first the regex pattern you want to search for. The second parameter is the string to replace the match or a function to do something - Example 1
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "The sky is blue."
- Example 2: access capture groups in the replacement string with dollar signs
$
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
- Write a regex fixRegex using three capture groups that will search for each word in the string “one two three”. Then update the replaceText variable to replace “one two three” with the string “three two one” and assign the result to the result variable. Make sure you are utilizing capture groups in the replacement string using the dollar sign ($) syntax
let str = "one two three";
let fixRegex = /(\w+)\s(\w+)\s(\w+)/; //
let replaceText = "$3 $2 $1"; //
let result = str.replace(fixRegex, replaceText);
9. Remove Whitespace from Start and End
- Sometimes whitespace characters around strings are not wanted but are there. Typical processing of strings is to remove the whitespace at the start and end of it.
- Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
- Note: The String.prototype.trim() method would work here, but you’ll need to complete this challenge using regular expressions.
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex, "") // Change this line