diff --git a/curriculum/challenges/english/25-front-end-development/quiz-javascript-regular-expressions/66edd3011f18f4ee1bd9d28b.md b/curriculum/challenges/english/25-front-end-development/quiz-javascript-regular-expressions/66edd3011f18f4ee1bd9d28b.md index 9a372d5f6d8..11a2ed7dee9 100644 --- a/curriculum/challenges/english/25-front-end-development/quiz-javascript-regular-expressions/66edd3011f18f4ee1bd9d28b.md +++ b/curriculum/challenges/english/25-front-end-development/quiz-javascript-regular-expressions/66edd3011f18f4ee1bd9d28b.md @@ -17,7 +17,7 @@ To pass the quiz, you must correctly answer at least 18 of the 20 questions belo #### --text-- -Which of the following is NOT a regular expression? +Which of the following is NOT a valid regular expression? #### --distractors-- @@ -99,23 +99,23 @@ A boolean indicating whether the string matches the regular expression. #### --text-- -What happens when `match` is used? +What does the `match` method do when passed a regular expression without any flags? #### --distractors-- -`match` searches for any strings that match the given regular expression, and returns ALL matches as an array. +It searches for any strings that match the given regular expression, and returns ALL matches as an array. --- -`match` searches for the first full match, and returns the starting index of that match. +It searches for the first full match, and returns the starting index of that match. --- -`match` searches for the first full match, and returns a boolean indicating whether or not a match was found. +It searches for the first full match, and returns a boolean indicating whether or not a match was found. #### --answer-- -`match` searches for the first full match, and returns an array containing that first match. +It searches for the first full match, and returns an array containing that first match. ### --question-- @@ -125,19 +125,19 @@ Which is the best use case for `test`? #### --distractors-- -`test` should be used for verifying a given regular expression. +For verifying a given regular expression. --- -`test` should be used for detailed information about a string match, such as the length of the matched string. +For getting detailed information about a string match, such as the length of the matched string. --- -`test` should be used to extract all matches from a given string. +To extract all matches from a given string. #### --answer-- -`test` should be used to check if a given string matches the regular expression. +To check if a given string matches the regular expression. ### --question-- @@ -147,19 +147,19 @@ What is the purpose of using `replace`? #### --distractors-- -`replace` is used to replace the current regular expression with a new regular expression. +To replace the current regular expression with a new regular expression. --- -When given an index and a string, `replace` replaces the character at the specified index with the string provided. +When given an index and a string, to replace the character at the specified index with the string provided. --- -`replace` is a helper method for replacing an entire string with another string. +To remove all instances of a matched substring from a string. #### --answer-- -`replace` replaces a matched string with a given replacement string. +To replace a matched string with a given replacement string. ### --question-- @@ -169,7 +169,7 @@ What is the difference between `match` and `matchAll`? #### --distractors-- -`match` is restricted to only one match, while `matchAll` captures all possible matches. +`match` always returns a string, while `matchAll` always returns an array of matches. --- @@ -239,7 +239,7 @@ Which of the following character classes is equivalent to the regular expression #### --text-- -Which of the following character classes is best used in a regular expression that finds phone numbers? +Which of the following character classes is most appropriate for extracting digits from a phone number? #### --distractors-- @@ -327,7 +327,7 @@ Which quantifier matches the preceding element zero or one times? #### --text-- -Which of the following regular expressions allows numbers between 0 and 999,999? +Which of the following regular expressions only allows numbers between 0 and 999,999? #### --distractors-- @@ -349,23 +349,23 @@ Which of the following regular expressions allows numbers between 0 and 999,999? #### --text-- -Which of the following statements is true about the `[]` character class? +Which of the following statements is true about the custom character class (`[]`)? #### --distractors-- -The `[]` character class is a set of characters to be removed from the match. +It's a set of characters to be removed from the match. --- -The `[]` character class can define a set of characters to match without the need to escape any special characters. +It can define a set of characters to match without the need to escape any special characters. --- -The `[]` character class represents a set of characters in Unicode form. +It represents a set of characters in Unicode form. #### --answer-- -The `[]` character class can define a custom set of characters to match. +It can define a custom set of characters to match. ### --question-- @@ -431,53 +431,60 @@ The non-capturing group is considered an optional match, so only a successful ma #### --answer-- -The non-capturing group attempts to match using the given subpattern, and continues without memorizing the result. +The non-capturing group attempts to match using the given subpattern, and continues without keeping the result in memory. ### --question-- #### --text-- -What happens when a backreference (`\1`, `\2`, etc.) is used in a regular expression? - -#### --distractors-- - -The backreference imports a different regular expression as a sub-pattern. - ---- - -The backreference copies the sub-pattern used in a previous capturing group. - ---- - -The backreference implicitly includes the match from a previous capture group to prevent the regular expression from failing. - -#### --answer-- - -The backreference includes the result from a capture group as part of a pattern/sub-pattern to match. - -### --question-- - -#### --text-- - -Which part of the following regular expression causes errors? +Given the following regular expression, which of these tests will return `true`? ```js -/(?:Expense|Asset) \$\d+\.\d{2} \1 \$\d+\.\d{2}/ +const regex = /(cat)\s+\1/i; ``` #### --distractors-- -The regular expression does not make use of the whitespace character class `\s`. +```js +regex.test("cat dog cat"); +``` --- -The regular expression allows 1 or more numbers after the $. +```js +regex.test("catcat"); +``` --- -The regular expression escapes the `.` and `$`. +```js +regex.test("cat\s+cat"); +``` #### --answer-- -The regular expression is attempting to use a backreference to a non-capturing group. +```js +regex.test("cat cat"); +``` +### --question-- + +#### --text-- + +What does the wildcard character class (`.`) do in a regular expression? + +#### --distractors-- + +It matches only whitespace characters. + +--- + +It matches the start of a string. + +--- + +It matches a single digit. + +#### --answer-- + +It matches any single character except a newline. diff --git a/curriculum/challenges/english/25-front-end-development/review-javascript-regular-expressions/6723cdfa4ae237bf6b7e32eb.md b/curriculum/challenges/english/25-front-end-development/review-javascript-regular-expressions/6723cdfa4ae237bf6b7e32eb.md index 4564f9ec023..7ce64ef7a16 100644 --- a/curriculum/challenges/english/25-front-end-development/review-javascript-regular-expressions/6723cdfa4ae237bf6b7e32eb.md +++ b/curriculum/challenges/english/25-front-end-development/review-javascript-regular-expressions/6723cdfa4ae237bf6b7e32eb.md @@ -200,6 +200,12 @@ const regex = /^\d{4}$/; const regex = /free(code)camp/i; ``` +- **Non-Capturing Groups**: A non-capturing group is similar to a capturing group but does not store the matched portion of the string for later use. Non-capturing groups are defined by `(?:...)`. + +```js +const regex = /free(?:code)camp/i; +``` + - **Backreferences**: A backreference in regular expressions refers to a way to reuse a part of the pattern that was matched earlier in the same expression. It allows you to refer to a captured group (a part of the pattern in parentheses) by its number. For example, `$1` refers to the first captured group. ```js @@ -207,6 +213,14 @@ const regex = /free(co+de)camp/i; console.log("freecoooooooodecamp".replace(regex, "paid$1world")); ``` +- You can use backreferences within the regex itself to match the same text captured by a previous group with a backslash and the capture group number. For example: + +```js +const regex = /(hello) \1/i; +console.log(regex.test("hello hello")); // true +console.log(regex.test("hello world")); // false +``` + # --assignment-- Review the JavaScript Regular Expressions topics and concepts.