chore(learn): audit javascript algorithms and data structures (#41092)
* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
committed by
GitHub
parent
57f3c80345
commit
7117919d36
@ -18,13 +18,15 @@ For example, there are slight differences in American and British English and yo
|
||||
let american = "color";
|
||||
let british = "colour";
|
||||
let rainbowRegex= /colou?r/;
|
||||
rainbowRegex.test(american); // Returns true
|
||||
rainbowRegex.test(british); // Returns true
|
||||
rainbowRegex.test(american);
|
||||
rainbowRegex.test(british);
|
||||
```
|
||||
|
||||
Both uses of the `test` method would return `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Change the regex `favRegex` to match both the American English (favorite) and the British English (favourite) version of the word.
|
||||
Change the regex `favRegex` to match both the American English (`favorite`) and the British English (`favourite`) version of the word.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -35,21 +37,21 @@ favRegex.lastIndex = 0;
|
||||
assert(favRegex.source.match(/\?/).length > 0);
|
||||
```
|
||||
|
||||
Your regex should match `"favorite"`
|
||||
Your regex should match the string `favorite`
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(favRegex.test('favorite'));
|
||||
```
|
||||
|
||||
Your regex should match `"favourite"`
|
||||
Your regex should match the string `favourite`
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(favRegex.test('favourite'));
|
||||
```
|
||||
|
||||
Your regex should not match `"fav"`
|
||||
Your regex should not match the string `fav`
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
|
@ -18,9 +18,10 @@ Then check whether the desired string groups are in the test string by using the
|
||||
let testStr = "Pumpkin";
|
||||
let testRegex = /P(engu|umpk)in/;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
The `test` method here would return `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
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.
|
||||
|
@ -16,13 +16,13 @@ Here's an example:
|
||||
|
||||
```js
|
||||
"Hello, World!".match(/Hello/);
|
||||
// Returns ["Hello"]
|
||||
let ourStr = "Regular expressions";
|
||||
let ourRegex = /expressions/;
|
||||
ourStr.match(ourRegex);
|
||||
// Returns ["expressions"]
|
||||
```
|
||||
|
||||
Here the first `match` would return `["Hello"]` and the second would return `["expressions"]`.
|
||||
|
||||
Note that the `.match` syntax is the "opposite" of the `.test` method you have been using thus far:
|
||||
|
||||
```js
|
||||
@ -32,17 +32,17 @@ Note that the `.match` syntax is the "opposite" of the `.test` method you have b
|
||||
|
||||
# --instructions--
|
||||
|
||||
Apply the `.match()` method to extract the word `coding`.
|
||||
Apply the `.match()` method to extract the string `coding`.
|
||||
|
||||
# --hints--
|
||||
|
||||
The `result` should have the word `coding`
|
||||
The `result` should have the string `coding`
|
||||
|
||||
```js
|
||||
assert(result.join() === 'coding');
|
||||
```
|
||||
|
||||
Your regex `codingRegex` should search for `coding`
|
||||
Your regex `codingRegex` should search for the string `coding`
|
||||
|
||||
```js
|
||||
assert(codingRegex.source === 'coding');
|
||||
|
@ -16,8 +16,7 @@ Regular expressions are by default greedy, so the match would return `["titani"]
|
||||
|
||||
However, you can use the `?` character to change it to lazy matching. `"titanic"` matched against the adjusted regex of `/t[a-z]*?i/` returns `["ti"]`.
|
||||
|
||||
**Note**
|
||||
Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.
|
||||
**Note:** Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.
|
||||
|
||||
# --instructions--
|
||||
|
||||
@ -37,7 +36,7 @@ assert(result[0] == '<h1>');
|
||||
assert(/\?/g.test(myRegex));
|
||||
```
|
||||
|
||||
`myRegex` should not include the string 'h1'
|
||||
`myRegex` should not include the string `h1`
|
||||
|
||||
```js
|
||||
assert(!myRegex.source.match('h1'));
|
||||
|
@ -14,20 +14,22 @@ So far, you have only been able to extract or search a pattern once.
|
||||
let testStr = "Repeat, Repeat, Repeat";
|
||||
let ourRegex = /Repeat/;
|
||||
testStr.match(ourRegex);
|
||||
// Returns ["Repeat"]
|
||||
```
|
||||
|
||||
Here `match` would return `["Repeat"]`.
|
||||
|
||||
To search or extract a pattern more than once, you can use the `g` flag.
|
||||
|
||||
```js
|
||||
let repeatRegex = /Repeat/g;
|
||||
testStr.match(repeatRegex);
|
||||
// Returns ["Repeat", "Repeat", "Repeat"]
|
||||
```
|
||||
|
||||
And here `match` returns the value `["Repeat", "Repeat", "Repeat"]`
|
||||
|
||||
# --instructions--
|
||||
|
||||
Using the regex `starRegex`, find and extract both `"Twinkle"` words from the string `twinkleStar`.
|
||||
Using the regex `starRegex`, find and extract both `Twinkle` words from the string `twinkleStar`.
|
||||
|
||||
**Note**
|
||||
You can have multiple flags on your regex like `/search/gi`
|
||||
@ -46,7 +48,7 @@ Your regex `starRegex` should use the case insensitive flag `i`
|
||||
assert(starRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
Your match should match both occurrences of the word `"Twinkle"`
|
||||
Your match should match both occurrences of the word `Twinkle`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -36,13 +36,13 @@ Write a greedy regex that finds one or more criminals within a group of other pe
|
||||
|
||||
# --hints--
|
||||
|
||||
Your regex should match one criminal (`C`) in `"C"`
|
||||
Your regex should match one criminal (`C`) in the string `C`
|
||||
|
||||
```js
|
||||
assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
|
||||
```
|
||||
|
||||
Your regex should match two criminals (`CC`) in `"CC"`
|
||||
Your regex should match two criminals (`CC`) in the string `CC`
|
||||
|
||||
```js
|
||||
assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
|
||||
@ -57,7 +57,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should match five criminals (`CCCCC`) in `"P6P2P7P4P5CCCCCP3P1"`
|
||||
Your regex should match five criminals (`CCCCC`) in the string `P6P2P7P4P5CCCCCP3P1`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -66,19 +66,19 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should not match any criminals in `""`
|
||||
Your regex should not match any criminals in the empty string `""`
|
||||
|
||||
```js
|
||||
assert(!reCriminals.test(''));
|
||||
```
|
||||
|
||||
Your regex should not match any criminals in `"P1P2P3"`
|
||||
Your regex should not match any criminals in the string `P1P2P3`
|
||||
|
||||
```js
|
||||
assert(!reCriminals.test('P1P2P3'));
|
||||
```
|
||||
|
||||
Your regex should match fifty criminals (`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`) in `"P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3"`.
|
||||
Your regex should match fifty criminals (`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`) in the string `P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -10,71 +10,71 @@ dashedName: ignore-case-while-matching
|
||||
|
||||
Up until now, you've looked at regexes to do literal matches of strings. But sometimes, you might want to also match case differences.
|
||||
|
||||
Case (or sometimes letter case) is the difference between uppercase letters and lowercase letters. Examples of uppercase are `"A"`, `"B"`, and `"C"`. Examples of lowercase are `"a"`, `"b"`, and `"c"`.
|
||||
Case (or sometimes letter case) is the difference between uppercase letters and lowercase letters. Examples of uppercase are `A`, `B`, and `C`. Examples of lowercase are `a`, `b`, and `c`.
|
||||
|
||||
You can match both cases using what is called a flag. There are other flags but here you'll focus on the flag that ignores case - the `i` flag. You can use it by appending it to the regex. An example of using this flag is `/ignorecase/i`. This regex can match the strings `"ignorecase"`, `"igNoreCase"`, and `"IgnoreCase"`.
|
||||
You can match both cases using what is called a flag. There are other flags but here you'll focus on the flag that ignores case - the `i` flag. You can use it by appending it to the regex. An example of using this flag is `/ignorecase/i`. This regex can match the strings `ignorecase`, `igNoreCase`, and `IgnoreCase`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a regex `fccRegex` to match `"freeCodeCamp"`, no matter its case. Your regex should not match any abbreviations or variations with spaces.
|
||||
Write a regex `fccRegex` to match `freeCodeCamp`, no matter its case. Your regex should not match any abbreviations or variations with spaces.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your regex should match `freeCodeCamp`
|
||||
Your regex should match the string `freeCodeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('freeCodeCamp'));
|
||||
```
|
||||
|
||||
Your regex should match `FreeCodeCamp`
|
||||
Your regex should match the string `FreeCodeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCodeCamp'));
|
||||
```
|
||||
|
||||
Your regex should match `FreecodeCamp`
|
||||
Your regex should match the string `FreecodeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreecodeCamp'));
|
||||
```
|
||||
|
||||
Your regex should match `FreeCodecamp`
|
||||
Your regex should match the string `FreeCodecamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCodecamp'));
|
||||
```
|
||||
|
||||
Your regex should not match `Free Code Camp`
|
||||
Your regex should not match the string `Free Code Camp`
|
||||
|
||||
```js
|
||||
assert(!fccRegex.test('Free Code Camp'));
|
||||
```
|
||||
|
||||
Your regex should match `FreeCOdeCamp`
|
||||
Your regex should match the string `FreeCOdeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCOdeCamp'));
|
||||
```
|
||||
|
||||
Your regex should not match `FCC`
|
||||
Your regex should not match the string `FCC`
|
||||
|
||||
```js
|
||||
assert(!fccRegex.test('FCC'));
|
||||
```
|
||||
|
||||
Your regex should match `FrEeCoDeCamp`
|
||||
Your regex should match the string `FrEeCoDeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FrEeCoDeCamp'));
|
||||
```
|
||||
|
||||
Your regex should match `FrEeCodECamp`
|
||||
Your regex should match the string `FrEeCodECamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FrEeCodECamp'));
|
||||
```
|
||||
|
||||
Your regex should match `FReeCodeCAmp`
|
||||
Your regex should match the string `FReeCodeCAmp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FReeCodeCAmp'));
|
||||
|
@ -8,57 +8,57 @@ dashedName: match-a-literal-string-with-different-possibilities
|
||||
|
||||
# --description--
|
||||
|
||||
Using regexes like `/coding/`, you can look for the pattern `"coding"` in another string.
|
||||
Using regexes like `/coding/`, you can look for the pattern `coding` in another string.
|
||||
|
||||
This is powerful to search single strings, but it's limited to only one pattern. You can search for multiple patterns using the `alternation` or `OR` operator: `|`.
|
||||
|
||||
This operator matches patterns either before or after it. For example, if you wanted to match `"yes"` or `"no"`, the regex you want is `/yes|no/`.
|
||||
This operator matches patterns either before or after it. For example, if you wanted to match the strings `yes` or `no`, the regex you want is `/yes|no/`.
|
||||
|
||||
You can also search for more than just two patterns. You can do this by adding more patterns with more `OR` operators separating them, like `/yes|no|maybe/`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Complete the regex `petRegex` to match the pets `"dog"`, `"cat"`, `"bird"`, or `"fish"`.
|
||||
Complete the regex `petRegex` to match the pets `dog`, `cat`, `bird`, or `fish`.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your regex `petRegex` should return `true` for the string `"John has a pet dog."`
|
||||
Your regex `petRegex` should return `true` for the string `John has a pet dog.`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('John has a pet dog.'));
|
||||
```
|
||||
|
||||
Your regex `petRegex` should return `false` for the string `"Emma has a pet rock."`
|
||||
Your regex `petRegex` should return `false` for the string `Emma has a pet rock.`
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Emma has a pet rock.'));
|
||||
```
|
||||
|
||||
Your regex `petRegex` should return `true` for the string `"Emma has a pet bird."`
|
||||
Your regex `petRegex` should return `true` for the string `Emma has a pet bird.`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Emma has a pet bird.'));
|
||||
```
|
||||
|
||||
Your regex `petRegex` should return `true` for the string `"Liz has a pet cat."`
|
||||
Your regex `petRegex` should return `true` for the string `Liz has a pet cat.`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Liz has a pet cat.'));
|
||||
```
|
||||
|
||||
Your regex `petRegex` should return `false` for the string `"Kara has a pet dolphin."`
|
||||
Your regex `petRegex` should return `false` for the string `Kara has a pet dolphin.`
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Kara has a pet dolphin.'));
|
||||
```
|
||||
|
||||
Your regex `petRegex` should return `true` for the string `"Alice has a pet fish."`
|
||||
Your regex `petRegex` should return `true` for the string `Alice has a pet fish.`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Alice has a pet fish.'));
|
||||
```
|
||||
|
||||
Your regex `petRegex` should return `false` for the string `"Jimmy has a pet computer."`
|
||||
Your regex `petRegex` should return `false` for the string `Jimmy has a pet computer.`
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Jimmy has a pet computer.'));
|
||||
|
@ -17,12 +17,14 @@ let longHand = /[A-Za-z0-9_]+/;
|
||||
let shortHand = /\w+/;
|
||||
let numbers = "42";
|
||||
let varNames = "important_var";
|
||||
longHand.test(numbers); // Returns true
|
||||
shortHand.test(numbers); // Returns true
|
||||
longHand.test(varNames); // Returns true
|
||||
shortHand.test(varNames); // Returns true
|
||||
longHand.test(numbers);
|
||||
shortHand.test(numbers);
|
||||
longHand.test(varNames);
|
||||
shortHand.test(varNames);
|
||||
```
|
||||
|
||||
All four of these `test` calls would return `true`.
|
||||
|
||||
These shortcut character classes are also known as <dfn>shorthand character classes</dfn>.
|
||||
|
||||
# --instructions--
|
||||
@ -43,7 +45,7 @@ Your regex should use the shorthand character `\w` to match all characters which
|
||||
assert(/\\w/.test(alphabetRegexV2.source));
|
||||
```
|
||||
|
||||
Your regex should find 31 alphanumeric characters in `"The five boxing wizards jump quickly."`
|
||||
Your regex should find 31 alphanumeric characters in the string `The five boxing wizards jump quickly.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -51,7 +53,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should find 32 alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
|
||||
Your regex should find 32 alphanumeric characters in the string `Pack my box with five dozen liquor jugs.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -60,7 +62,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should find 30 alphanumeric characters in `"How vexingly quick daft zebras jump!"`
|
||||
Your regex should find 30 alphanumeric characters in the string `How vexingly quick daft zebras jump!`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -68,7 +70,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should find 36 alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
|
||||
Your regex should find 36 alphanumeric characters in the string `123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -30,37 +30,37 @@ Your regex should use the global flag.
|
||||
assert(noNumRegex.global);
|
||||
```
|
||||
|
||||
Your regex should find no non-digits in `"9"`.
|
||||
Your regex should find no non-digits in the string `9`.
|
||||
|
||||
```js
|
||||
assert('9'.match(noNumRegex) == null);
|
||||
```
|
||||
|
||||
Your regex should find 6 non-digits in `"Catch 22"`.
|
||||
Your regex should find 6 non-digits in the string `Catch 22`.
|
||||
|
||||
```js
|
||||
assert('Catch 22'.match(noNumRegex).length == 6);
|
||||
```
|
||||
|
||||
Your regex should find 11 non-digits in `"101 Dalmatians"`.
|
||||
Your regex should find 11 non-digits in the string `101 Dalmatians`.
|
||||
|
||||
```js
|
||||
assert('101 Dalmatians'.match(noNumRegex).length == 11);
|
||||
```
|
||||
|
||||
Your regex should find 15 non-digits in `"One, Two, Three"`.
|
||||
Your regex should find 15 non-digits in the string `One, Two, Three`.
|
||||
|
||||
```js
|
||||
assert('One, Two, Three'.match(noNumRegex).length == 15);
|
||||
```
|
||||
|
||||
Your regex should find 12 non-digits in `"21 Jump Street"`.
|
||||
Your regex should find 12 non-digits in the string `21 Jump Street`.
|
||||
|
||||
```js
|
||||
assert('21 Jump Street'.match(noNumRegex).length == 12);
|
||||
```
|
||||
|
||||
Your regex should find 17 non-digits in `"2001: A Space Odyssey"`.
|
||||
Your regex should find 17 non-digits in the string `2001: A Space Odyssey`.
|
||||
|
||||
```js
|
||||
assert('2001: A Space Odyssey'.match(noNumRegex).length == 17);
|
||||
|
@ -30,37 +30,37 @@ Your regex should use the global flag.
|
||||
assert(numRegex.global);
|
||||
```
|
||||
|
||||
Your regex should find 1 digit in `"9"`.
|
||||
Your regex should find 1 digit in the string `9`.
|
||||
|
||||
```js
|
||||
assert('9'.match(numRegex).length == 1);
|
||||
```
|
||||
|
||||
Your regex should find 2 digits in `"Catch 22"`.
|
||||
Your regex should find 2 digits in the string `Catch 22`.
|
||||
|
||||
```js
|
||||
assert('Catch 22'.match(numRegex).length == 2);
|
||||
```
|
||||
|
||||
Your regex should find 3 digits in `"101 Dalmatians"`.
|
||||
Your regex should find 3 digits in the string `101 Dalmatians`.
|
||||
|
||||
```js
|
||||
assert('101 Dalmatians'.match(numRegex).length == 3);
|
||||
```
|
||||
|
||||
Your regex should find no digits in `"One, Two, Three"`.
|
||||
Your regex should find no digits in the string `One, Two, Three`.
|
||||
|
||||
```js
|
||||
assert('One, Two, Three'.match(numRegex) == null);
|
||||
```
|
||||
|
||||
Your regex should find 2 digits in `"21 Jump Street"`.
|
||||
Your regex should find 2 digits in the string `21 Jump Street`.
|
||||
|
||||
```js
|
||||
assert('21 Jump Street'.match(numRegex).length == 2);
|
||||
```
|
||||
|
||||
Your regex should find 4 digits in `"2001: A Space Odyssey"`.
|
||||
Your regex should find 4 digits in the string `2001: A Space Odyssey`.
|
||||
|
||||
```js
|
||||
assert('2001: A Space Odyssey'.match(numRegex).length == 4);
|
||||
|
@ -10,19 +10,21 @@ dashedName: 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 called `dot` and `period`. 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.
|
||||
The wildcard character `.` will match any one character. The wildcard is also called `dot` and `period`. 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.
|
||||
|
||||
```js
|
||||
let humStr = "I'll hum a song";
|
||||
let hugStr = "Bear hug";
|
||||
let huRegex = /hu./;
|
||||
huRegex.test(humStr); // Returns true
|
||||
huRegex.test(hugStr); // Returns true
|
||||
huRegex.test(humStr);
|
||||
huRegex.test(hugStr);
|
||||
```
|
||||
|
||||
Both of these `test` calls would return `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Complete the regex `unRegex` so that it matches the strings `"run"`, `"sun"`, `"fun"`, `"pun"`, `"nun"`, and `"bun"`. Your regex should use the wildcard character.
|
||||
Complete the regex `unRegex` so that it matches the strings `run`, `sun`, `fun`, `pun`, `nun`, and `bun`. Your regex should use the wildcard character.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -38,42 +40,42 @@ You should use the wildcard character in your regex `unRegex`
|
||||
assert(/\./.test(unRegex.source));
|
||||
```
|
||||
|
||||
Your regex `unRegex` should match `"run"` in `"Let us go on a run."`
|
||||
Your regex `unRegex` should match `run` in the string `Let us go on a run.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('Let us go on a run.'));
|
||||
```
|
||||
|
||||
Your regex `unRegex` should match `"sun"` in `"The sun is out today."`
|
||||
Your regex `unRegex` should match `sun` in the string `The sun is out today.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('The sun is out today.'));
|
||||
```
|
||||
|
||||
Your regex `unRegex` should match `"fun"` in `"Coding is a lot of fun."`
|
||||
Your regex `unRegex` should match `fun` in the string `Coding is a lot of fun.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('Coding is a lot of fun.'));
|
||||
```
|
||||
|
||||
Your regex `unRegex` should match `"pun"` in `"Seven days without a pun makes one weak."`
|
||||
Your regex `unRegex` should match `pun` in the string `Seven days without a pun makes one weak.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('Seven days without a pun makes one weak.'));
|
||||
```
|
||||
|
||||
Your regex `unRegex` should match `"nun"` in `"One takes a vow to be a nun."`
|
||||
Your regex `unRegex` should match `nun` in the string `One takes a vow to be a nun.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('One takes a vow to be a nun.'));
|
||||
```
|
||||
|
||||
Your regex `unRegex` should match `"bun"` in `"She got fired from the hot dog stand for putting her hair in a bun."`
|
||||
Your regex `unRegex` should match `bun` in the string `She got fired from the hot dog stand for putting her hair in a bun.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
@ -84,14 +86,14 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex `unRegex` should not match `"There is a bug in my code."`
|
||||
Your regex `unRegex` should not match the string `There is a bug in my code.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(!unRegex.test('There is a bug in my code.'));
|
||||
```
|
||||
|
||||
Your regex `unRegex` should not match `"Catch me if you can."`
|
||||
Your regex `unRegex` should not match the string `Catch me if you can.`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
|
@ -16,19 +16,19 @@ In an earlier challenge, you used the caret character (`^`) inside a character s
|
||||
let firstString = "Ricky is first and can be found.";
|
||||
let firstRegex = /^Ricky/;
|
||||
firstRegex.test(firstString);
|
||||
// Returns true
|
||||
let notFirst = "You can't find Ricky now.";
|
||||
firstRegex.test(notFirst);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
The first `test` call would return `true`, while the second would return `false`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the caret character in a regex to find `"Cal"` only in the beginning of the string `rickyAndCal`.
|
||||
Use the caret character in a regex to find `Cal` only in the beginning of the string `rickyAndCal`.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your regex should search for `"Cal"` with a capital letter.
|
||||
Your regex should search for the string `Cal` with a capital letter.
|
||||
|
||||
```js
|
||||
assert(calRegex.source == '^Cal');
|
||||
@ -40,13 +40,13 @@ Your regex should not use any flags.
|
||||
assert(calRegex.flags == '');
|
||||
```
|
||||
|
||||
Your regex should match `"Cal"` at the beginning of the string.
|
||||
Your regex should match the string `Cal` at the beginning of the string.
|
||||
|
||||
```js
|
||||
assert(calRegex.test('Cal and Ricky both like racing.'));
|
||||
```
|
||||
|
||||
Your regex should not match `"Cal"` in the middle of a string.
|
||||
Your regex should not match the string `Cal` in the middle of a string.
|
||||
|
||||
```js
|
||||
assert(!calRegex.test('Ricky and Cal both like racing.'));
|
||||
|
@ -12,13 +12,13 @@ Sometimes, you need to match a character (or group of characters) that appears o
|
||||
|
||||
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.
|
||||
|
||||
For example, `/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"]`.
|
||||
For example, `/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 the `a` characters are not in a row - there is a `b` between them. Finally, since there is no `"a"` in the string `"bcd"`, it wouldn't find a match.
|
||||
If it were instead checking the string `abab`, it would find two matches and return `["a", "a"]` because the `a` characters are not in a row - there is a `b` between them. Finally, since there is no `a` in the string `bcd`, it wouldn't find a match.
|
||||
|
||||
# --instructions--
|
||||
|
||||
You want to find matches when the letter `s` occurs one or more times in `"Mississippi"`. Write a regex that uses the `+` sign.
|
||||
You want to find matches when the letter `s` occurs one or more times in `Mississippi`. Write a regex that uses the `+` sign.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -34,7 +34,7 @@ Your regex `myRegex` should match 2 items.
|
||||
assert(result.length == 2);
|
||||
```
|
||||
|
||||
The `result` variable should be an array with two matches of `"ss"`
|
||||
The `result` variable should be an array with two matches of `ss`
|
||||
|
||||
```js
|
||||
assert(result[0] == 'ss' && result[1] == 'ss');
|
||||
|
@ -17,14 +17,16 @@ 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
|
||||
soccerWord.match(goRegex);
|
||||
gPhrase.match(goRegex);
|
||||
oPhrase.match(goRegex);
|
||||
```
|
||||
|
||||
In order, the three `match` calls would return the values `["goooooooo"]`, `["g"]`, and `null`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
For this challenge, `chewieQuote` has been initialized as "Aaaaaaaaaaaaaaaarrrgh!" behind the scenes. 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.
|
||||
For this challenge, `chewieQuote` has been initialized as the string `Aaaaaaaaaaaaaaaarrrgh!` behind the scenes. 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.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -34,13 +36,13 @@ Your regex `chewieRegex` should use the `*` character to match zero or more `a`
|
||||
assert(/\*/.test(chewieRegex.source));
|
||||
```
|
||||
|
||||
Your regex should match `"A"` in `chewieQuote`.
|
||||
Your regex should match the string `A` in `chewieQuote`.
|
||||
|
||||
```js
|
||||
assert(result[0][0] === 'A');
|
||||
```
|
||||
|
||||
Your regex should match `"Aaaaaaaaaaaaaaaa"` in `chewieQuote`.
|
||||
Your regex should match the string `Aaaaaaaaaaaaaaaa` in `chewieQuote`.
|
||||
|
||||
```js
|
||||
assert(result[0] === 'Aaaaaaaaaaaaaaaa');
|
||||
@ -52,7 +54,7 @@ Your regex `chewieRegex` should match 16 characters in `chewieQuote`.
|
||||
assert(result[0].length === 16);
|
||||
```
|
||||
|
||||
Your regex should not match any characters in "He made a fair move. Screaming about it can't help you."
|
||||
Your regex should not match any characters in the string `He made a fair move. Screaming about it can't help you.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -60,7 +62,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should not match any characters in "Let him have it. It's not wise to upset a Wookiee."
|
||||
Your regex should not match any characters in the string `Let him have it. It's not wise to upset a Wookiee.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -16,20 +16,19 @@ You can search the end of strings using the dollar sign character `$` at the end
|
||||
let theEnding = "This is a never ending story";
|
||||
let storyRegex = /story$/;
|
||||
storyRegex.test(theEnding);
|
||||
// Returns true
|
||||
let noEnding = "Sometimes a story will have to end";
|
||||
storyRegex.test(noEnding);
|
||||
// Returns false
|
||||
|
||||
```
|
||||
|
||||
The first `test` call would return `true`, while the second would return `false`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the anchor character (`$`) to match the string `"caboose"` at the end of the string `caboose`.
|
||||
Use the anchor character (`$`) to match the string `caboose` at the end of the string `caboose`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should search for `"caboose"` with the dollar sign `$` anchor in your regex.
|
||||
You should search for `caboose` with the dollar sign `$` anchor in your regex.
|
||||
|
||||
```js
|
||||
assert(lastRegex.source == 'caboose$');
|
||||
@ -41,7 +40,7 @@ Your regex should not use any flags.
|
||||
assert(lastRegex.flags == '');
|
||||
```
|
||||
|
||||
You should match `"caboose"` at the end of the string `"The last car on a train is the caboose"`
|
||||
You should match `caboose` at the end of the string `The last car on a train is the caboose`
|
||||
|
||||
```js
|
||||
assert(lastRegex.test('The last car on a train is the caboose'));
|
||||
|
@ -16,10 +16,12 @@ You can search for the opposite of the `\w` with `\W`. Note, the opposite patter
|
||||
let shortHand = /\W/;
|
||||
let numbers = "42%";
|
||||
let sentence = "Coding!";
|
||||
numbers.match(shortHand); // Returns ["%"]
|
||||
sentence.match(shortHand); // Returns ["!"]
|
||||
numbers.match(shortHand);
|
||||
sentence.match(shortHand);
|
||||
```
|
||||
|
||||
The first `match` call would return the value `["%"]` and the second would return `["!"]`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the shorthand character class `\W` to count the number of non-alphanumeric characters in various quotes and strings.
|
||||
@ -32,7 +34,7 @@ Your regex should use the global flag.
|
||||
assert(nonAlphabetRegex.global);
|
||||
```
|
||||
|
||||
Your regex should find 6 non-alphanumeric characters in `"The five boxing wizards jump quickly."`.
|
||||
Your regex should find 6 non-alphanumeric characters in the string `The five boxing wizards jump quickly.`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -46,7 +48,7 @@ Your regex should use the shorthand character to match characters which are non-
|
||||
assert(/\\W/.test(nonAlphabetRegex.source));
|
||||
```
|
||||
|
||||
Your regex should find 8 non-alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
|
||||
Your regex should find 8 non-alphanumeric characters in the string `Pack my box with five dozen liquor jugs.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -54,7 +56,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should find 6 non-alphanumeric characters in `"How vexingly quick daft zebras jump!"`
|
||||
Your regex should find 6 non-alphanumeric characters in the string `How vexingly quick daft zebras jump!`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -62,7 +64,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should find 12 non-alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
|
||||
Your regex should find 12 non-alphanumeric characters in the string `123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -19,11 +19,13 @@ 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
|
||||
catStr.match(bgRegex);
|
||||
batStr.match(bgRegex);
|
||||
matStr.match(bgRegex);
|
||||
```
|
||||
|
||||
In order, the three `match` calls would return the values `["cat"]`, `["bat"]`, and `null`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Match all the letters in the string `quoteSample`.
|
||||
|
@ -8,23 +8,25 @@ dashedName: match-literal-strings
|
||||
|
||||
# --description--
|
||||
|
||||
In the last challenge, you searched for the word `"Hello"` using the regular expression `/Hello/`. That regex searched for a literal match of the string `"Hello"`. Here's another example searching for a literal match of the string `"Kevin"`:
|
||||
In the last challenge, you searched for the word `Hello` using the regular expression `/Hello/`. That regex searched for a literal match of the string `Hello`. Here's another example searching for a literal match of the string `Kevin`:
|
||||
|
||||
```js
|
||||
let testStr = "Hello, my name is Kevin.";
|
||||
let testRegex = /Kevin/;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
Any other forms of `"Kevin"` will not match. For example, the regex `/Kevin/` will not match `"kevin"` or `"KEVIN"`.
|
||||
This `test` call will return `true`.
|
||||
|
||||
Any other forms of `Kevin` will not match. For example, the regex `/Kevin/` will not match `kevin` or `KEVIN`.
|
||||
|
||||
```js
|
||||
let wrongRegex = /kevin/;
|
||||
wrongRegex.test(testStr);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
This `test` call will return `false`.
|
||||
|
||||
A future challenge will show how to match those other forms as well.
|
||||
|
||||
# --instructions--
|
||||
@ -33,7 +35,7 @@ Complete the regex `waldoRegex` to find `"Waldo"` in the string `waldoIsHiding`
|
||||
|
||||
# --hints--
|
||||
|
||||
Your regex `waldoRegex` should find `"Waldo"`
|
||||
Your regex `waldoRegex` should find the string `Waldo`
|
||||
|
||||
```js
|
||||
assert(waldoRegex.test(waldoIsHiding));
|
||||
|
@ -15,9 +15,11 @@ Search for non-whitespace using `\S`, which is an uppercase `s`. This pattern wi
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
let nonSpaceRegex = /\S/g;
|
||||
whiteSpace.match(nonSpaceRegex).length; // Returns 32
|
||||
whiteSpace.match(nonSpaceRegex).length;
|
||||
```
|
||||
|
||||
The value returned by the `.length` method would be `32`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Change the regex `countNonWhiteSpace` to look for multiple non-whitespace characters in a string.
|
||||
@ -36,7 +38,7 @@ Your regex should use the shorthand character `\S` to match all non-whitespace c
|
||||
assert(/\\S/.test(countNonWhiteSpace.source));
|
||||
```
|
||||
|
||||
Your regex should find 35 non-spaces in `"Men are from Mars and women are from Venus."`
|
||||
Your regex should find 35 non-spaces in the string `Men are from Mars and women are from Venus.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -45,13 +47,13 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should find 23 non-spaces in `"Space: the final frontier."`
|
||||
Your regex should find 23 non-spaces in the string `Space: the final frontier.`
|
||||
|
||||
```js
|
||||
assert('Space: the final frontier.'.match(countNonWhiteSpace).length == 23);
|
||||
```
|
||||
|
||||
Your regex should find 21 non-spaces in `"MindYourPersonalSpace"`
|
||||
Your regex should find 21 non-spaces in the string `MindYourPersonalSpace`
|
||||
|
||||
```js
|
||||
assert('MindYourPersonalSpace'.match(countNonWhiteSpace).length == 21);
|
||||
|
@ -17,7 +17,6 @@ Also, it is possible to combine a range of letters and numbers in a single chara
|
||||
```js
|
||||
let jennyStr = "Jenny8675309";
|
||||
let myRegex = /[a-z0-9]/ig;
|
||||
// matches all letters and numbers in jennyStr
|
||||
jennyStr.match(myRegex);
|
||||
```
|
||||
|
||||
|
@ -12,7 +12,7 @@ You learned how to match literal patterns (`/literal/`) and wildcard character (
|
||||
|
||||
You can search for a literal pattern with some flexibility with <dfn>character classes</dfn>. Character classes allow you to define a group of characters you wish to match by placing them inside square (`[` and `]`) brackets.
|
||||
|
||||
For example, you want to match `"bag"`, `"big"`, and `"bug"` but not `"bog"`. You can create the regex `/b[aiu]g/` to do this. The `[aiu]` is the character class that will only match the characters `"a"`, `"i"`, or `"u"`.
|
||||
For example, you want to match `bag`, `big`, and `bug` but not `bog`. You can create the regex `/b[aiu]g/` to do this. The `[aiu]` is the character class that will only match the characters `a`, `i`, or `u`.
|
||||
|
||||
```js
|
||||
let bigStr = "big";
|
||||
@ -20,18 +20,19 @@ 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
|
||||
bigStr.match(bgRegex);
|
||||
bagStr.match(bgRegex);
|
||||
bugStr.match(bgRegex);
|
||||
bogStr.match(bgRegex);
|
||||
```
|
||||
|
||||
In order, the four `match` calls would return the values `["big"]`, `["bag"]`, `["bug"]`, and `null`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use a character class with vowels (`a`, `e`, `i`, `o`, `u`) in your regex `vowelRegex` to find all the vowels in the string `quoteSample`.
|
||||
|
||||
**Note**
|
||||
Be sure to match both upper- and lowercase vowels.
|
||||
**Note:** Be sure to match both upper- and lowercase vowels.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
@ -16,9 +16,9 @@ You can search for whitespace using `\s`, which is a lowercase `s`. This pattern
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
let spaceRegex = /\s/g;
|
||||
whiteSpace.match(spaceRegex);
|
||||
// Returns [" ", " "]
|
||||
```
|
||||
|
||||
This `match` call would return `[" ", " "]`.
|
||||
# --instructions--
|
||||
|
||||
Change the regex `countWhiteSpace` to look for multiple whitespace characters in a string.
|
||||
@ -37,7 +37,7 @@ Your regex should use the shorthand character `\s` to match all whitespace chara
|
||||
assert(/\\s/.test(countWhiteSpace.source));
|
||||
```
|
||||
|
||||
Your regex should find eight spaces in `"Men are from Mars and women are from Venus."`
|
||||
Your regex should find eight spaces in the string `Men are from Mars and women are from Venus.`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -46,13 +46,13 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Your regex should find three spaces in `"Space: the final frontier."`
|
||||
Your regex should find three spaces in the string `Space: the final frontier.`
|
||||
|
||||
```js
|
||||
assert('Space: the final frontier.'.match(countWhiteSpace).length == 3);
|
||||
```
|
||||
|
||||
Your regex should find no spaces in `"MindYourPersonalSpace"`
|
||||
Your regex should find no spaces in the string `MindYourPersonalSpace`
|
||||
|
||||
```js
|
||||
assert('MindYourPersonalSpace'.match(countWhiteSpace) == null);
|
||||
|
@ -23,16 +23,18 @@ 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(quRegex);
|
||||
noquit.match(qRegex);
|
||||
```
|
||||
|
||||
Both of these `match` calls would return `["q"]`.
|
||||
|
||||
A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
|
||||
|
||||
```js
|
||||
let password = "abc123";
|
||||
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
|
||||
checkPass.test(password); // Returns true
|
||||
checkPass.test(password);
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
@ -47,49 +49,49 @@ Your regex should use two positive `lookaheads`.
|
||||
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
||||
```
|
||||
|
||||
Your regex should not match `"astronaut"`
|
||||
Your regex should not match the string `astronaut`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('astronaut'));
|
||||
```
|
||||
|
||||
Your regex should not match `"banan1"`
|
||||
Your regex should not match the string `banan1`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('banan1'));
|
||||
```
|
||||
|
||||
Your regex should match `"bana12"`
|
||||
Your regex should match the string `bana12`
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('bana12'));
|
||||
```
|
||||
|
||||
Your regex should match `"abc123"`
|
||||
Your regex should match the string `abc123`
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('abc123'));
|
||||
```
|
||||
|
||||
Your regex should not match `"12345"`
|
||||
Your regex should not match the string `12345`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('12345'));
|
||||
```
|
||||
|
||||
Your regex should match `"8pass99"`
|
||||
Your regex should match the string `8pass99`
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('8pass99'));
|
||||
```
|
||||
|
||||
Your regex should not match `"1a2bcde"`
|
||||
Your regex should not match the string `1a2bcde`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('1a2bcde'));
|
||||
```
|
||||
|
||||
Your regex should match `"astr1on11aut"`
|
||||
Your regex should match the string `astr1on11aut`
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('astr1on11aut'));
|
||||
|
@ -18,7 +18,7 @@ Write a regex and use the appropriate string methods to remove whitespace at the
|
||||
|
||||
# --hints--
|
||||
|
||||
`result` should equal to `"Hello, World!"`
|
||||
`result` should be equal to the string `Hello, World!`
|
||||
|
||||
```js
|
||||
assert(result == 'Hello, World!');
|
||||
|
@ -26,79 +26,79 @@ Change the regex `userCheck` to fit the constraints listed above.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your regex should match `JACK`
|
||||
Your regex should match the string `JACK`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('JACK'));
|
||||
```
|
||||
|
||||
Your regex should not match `J`
|
||||
Your regex should not match the string `J`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('J'));
|
||||
```
|
||||
|
||||
Your regex should match `Jo`
|
||||
Your regex should match the string `Jo`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Jo'));
|
||||
```
|
||||
|
||||
Your regex should match `Oceans11`
|
||||
Your regex should match the string `Oceans11`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Oceans11'));
|
||||
```
|
||||
|
||||
Your regex should match `RegexGuru`
|
||||
Your regex should match the string `RegexGuru`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('RegexGuru'));
|
||||
```
|
||||
|
||||
Your regex should not match `007`
|
||||
Your regex should not match the string `007`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('007'));
|
||||
```
|
||||
|
||||
Your regex should not match `9`
|
||||
Your regex should not match the string `9`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('9'));
|
||||
```
|
||||
|
||||
Your regex should not match `A1`
|
||||
Your regex should not match the string `A1`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('A1'));
|
||||
```
|
||||
|
||||
Your regex should not match `BadUs3rnam3`
|
||||
Your regex should not match the string `BadUs3rnam3`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('BadUs3rnam3'));
|
||||
```
|
||||
|
||||
Your regex should match `Z97`
|
||||
Your regex should match the string `Z97`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Z97'));
|
||||
```
|
||||
|
||||
Your regex should not match `c57bT3`
|
||||
Your regex should not match the string `c57bT3`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('c57bT3'));
|
||||
```
|
||||
|
||||
Your regex should match `AB1`
|
||||
Your regex should match the string `AB1`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('AB1'));
|
||||
```
|
||||
|
||||
Your regex should not match `J%4`
|
||||
Your regex should not match the string `J%4`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('J%4'))
|
||||
|
@ -19,10 +19,12 @@ The example below matches any word that occurs twice separated by a space:
|
||||
```js
|
||||
let repeatStr = "regex regex";
|
||||
let repeatRegex = /(\w+)\s\1/;
|
||||
repeatRegex.test(repeatStr); // Returns true
|
||||
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
|
||||
repeatRegex.test(repeatStr);
|
||||
repeatStr.match(repeatRegex);
|
||||
```
|
||||
|
||||
The `test` call would return `true`, and the `match` call would return `["regex regex", "regex"]`.
|
||||
|
||||
Using the `.match()` method on a string will return an array with the string it matches, along with its capture group.
|
||||
|
||||
# --instructions--
|
||||
@ -43,43 +45,43 @@ Your regex should reuse a capture group twice.
|
||||
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
||||
```
|
||||
|
||||
Your regex should match `"42 42 42"`.
|
||||
Your regex should match the string `42 42 42`.
|
||||
|
||||
```js
|
||||
assert(reRegex.test('42 42 42'));
|
||||
```
|
||||
|
||||
Your regex should match `"100 100 100"`.
|
||||
Your regex should match the string `100 100 100`.
|
||||
|
||||
```js
|
||||
assert(reRegex.test('100 100 100'));
|
||||
```
|
||||
|
||||
Your regex should not match `"42 42 42 42"`.
|
||||
Your regex should not match the string `42 42 42 42`.
|
||||
|
||||
```js
|
||||
assert.equal('42 42 42 42'.match(reRegex.source), null);
|
||||
```
|
||||
|
||||
Your regex should not match `"42 42"`.
|
||||
Your regex should not match the string `42 42`.
|
||||
|
||||
```js
|
||||
assert.equal('42 42'.match(reRegex.source), null);
|
||||
```
|
||||
|
||||
Your regex should not match `"101 102 103"`.
|
||||
Your regex should not match the string `101 102 103`.
|
||||
|
||||
```js
|
||||
assert(!reRegex.test('101 102 103'));
|
||||
```
|
||||
|
||||
Your regex should not match `"1 2 3"`.
|
||||
Your regex should not match the string `1 2 3`.
|
||||
|
||||
```js
|
||||
assert(!reRegex.test('1 2 3'));
|
||||
```
|
||||
|
||||
Your regex should match `"10 10 10"`.
|
||||
Your regex should match the string `10 10 10`.
|
||||
|
||||
```js
|
||||
assert(reRegex.test('10 10 10'));
|
||||
|
@ -12,21 +12,23 @@ You can specify the lower and upper number of patterns with quantity specifiers
|
||||
|
||||
To specify a certain number of patterns, just have that one number between the curly brackets.
|
||||
|
||||
For example, to match only the word `"hah"` with the letter `a` `3` times, your regex would be `/ha{3}h/`.
|
||||
For example, to match only the word `hah` with the letter `a` `3` times, your regex would be `/ha{3}h/`.
|
||||
|
||||
```js
|
||||
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
|
||||
multipleHA.test(A4);
|
||||
multipleHA.test(A3);
|
||||
multipleHA.test(A100);
|
||||
```
|
||||
|
||||
In order, the three `test` calls would return `false`, `true`, and `false`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Change the regex `timRegex` to match the word `"Timber"` only when it has four letter `m`'s.
|
||||
Change the regex `timRegex` to match the word `Timber` only when it has four letter `m`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -36,35 +38,35 @@ Your regex should use curly brackets.
|
||||
assert(timRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
Your regex should not match `"Timber"`
|
||||
Your regex should not match the string `Timber`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Timber'));
|
||||
```
|
||||
|
||||
Your regex should not match `"Timmber"`
|
||||
Your regex should not match the string `Timmber`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Timmber'));
|
||||
```
|
||||
|
||||
Your regex should not match `"Timmmber"`
|
||||
Your regex should not match the string `Timmmber`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Timmmber'));
|
||||
```
|
||||
|
||||
Your regex should match `"Timmmmber"`
|
||||
Your regex should match the string `Timmmmber`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(timRegex.test('Timmmmber'));
|
||||
```
|
||||
|
||||
Your regex should not match `"Timber"` with 30 `m`'s in it.
|
||||
Your regex should not match the string `Timber` with 30 `m`'s in it.
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
|
@ -12,21 +12,23 @@ You can specify the lower and upper number of patterns with quantity specifiers
|
||||
|
||||
To only specify the lower number of patterns, keep the first number followed by a comma.
|
||||
|
||||
For example, to match only the string `"hah"` with the letter `a` appearing at least `3` times, your regex would be `/ha{3,}h/`.
|
||||
For example, to match only the string `hah` with the letter `a` appearing at least `3` times, your regex would be `/ha{3,}h/`.
|
||||
|
||||
```js
|
||||
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
|
||||
multipleA.test(A4);
|
||||
multipleA.test(A2);
|
||||
multipleA.test(A100);
|
||||
```
|
||||
|
||||
In order, the three `test` calls would return `true`, `false`, and `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Change the regex `haRegex` to match the word `"Hazzah"` only when it has four or more letter `z`'s.
|
||||
Change the regex `haRegex` to match the word `Hazzah` only when it has four or more letter `z`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -36,37 +38,37 @@ Your regex should use curly brackets.
|
||||
assert(haRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
Your regex should not match `"Hazzah"`
|
||||
Your regex should not match the string `Hazzah`
|
||||
|
||||
```js
|
||||
assert(!haRegex.test('Hazzah'));
|
||||
```
|
||||
|
||||
Your regex should not match `"Hazzzah"`
|
||||
Your regex should not match the string `Hazzzah`
|
||||
|
||||
```js
|
||||
assert(!haRegex.test('Hazzzah'));
|
||||
```
|
||||
|
||||
Your regex should match `"Hazzzzah"`
|
||||
Your regex should match the string `Hazzzzah`
|
||||
|
||||
```js
|
||||
assert('Hazzzzah'.match(haRegex)[0].length === 8);
|
||||
```
|
||||
|
||||
Your regex should match `"Hazzzzzah"`
|
||||
Your regex should match the string `Hazzzzzah`
|
||||
|
||||
```js
|
||||
assert('Hazzzzzah'.match(haRegex)[0].length === 9);
|
||||
```
|
||||
|
||||
Your regex should match `"Hazzzzzzah"`
|
||||
Your regex should match the string `Hazzzzzzah`
|
||||
|
||||
```js
|
||||
assert('Hazzzzzzah'.match(haRegex)[0].length === 10);
|
||||
```
|
||||
|
||||
Your regex should match `"Hazzah"` with 30 `z`'s in it.
|
||||
Your regex should match the string `Hazzah` with 30 `z`'s in it.
|
||||
|
||||
```js
|
||||
assert('Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah'.match(haRegex)[0].length === 34);
|
||||
|
@ -12,19 +12,21 @@ Recall that you use the plus sign `+` to look for one or more characters and the
|
||||
|
||||
You can specify the lower and upper number of patterns with <dfn>quantity specifiers</dfn>. Quantity specifiers are used with curly brackets (`{` and `}`). You put two numbers between the curly brackets - for the lower and upper number of patterns.
|
||||
|
||||
For example, to match only the letter `a` appearing between `3` and `5` times in the string `"ah"`, your regex would be `/a{3,5}h/`.
|
||||
For example, to match only the letter `a` appearing between `3` and `5` times in the string `ah`, your regex would be `/a{3,5}h/`.
|
||||
|
||||
```js
|
||||
let A4 = "aaaah";
|
||||
let A2 = "aah";
|
||||
let multipleA = /a{3,5}h/;
|
||||
multipleA.test(A4); // Returns true
|
||||
multipleA.test(A2); // Returns false
|
||||
multipleA.test(A4);
|
||||
multipleA.test(A2);
|
||||
```
|
||||
|
||||
The first `test` call would return `true`, while the second would return `false`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Change the regex `ohRegex` to match the entire phrase `"Oh no"` only when it has `3` to `6` letter `h`'s.
|
||||
Change the regex `ohRegex` to match the entire phrase `Oh no` only when it has `3` to `6` letter `h`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -34,37 +36,37 @@ Your regex should use curly brackets.
|
||||
assert(ohRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
Your regex should not match `"Ohh no"`
|
||||
Your regex should not match the string `Ohh no`
|
||||
|
||||
```js
|
||||
assert(!ohRegex.test('Ohh no'));
|
||||
```
|
||||
|
||||
Your regex should match `"Ohhh no"`
|
||||
Your regex should match the string `Ohhh no`
|
||||
|
||||
```js
|
||||
assert('Ohhh no'.match(ohRegex)[0].length === 7);
|
||||
```
|
||||
|
||||
Your regex should match `"Ohhhh no"`
|
||||
Your regex should match the string `Ohhhh no`
|
||||
|
||||
```js
|
||||
assert('Ohhhh no'.match(ohRegex)[0].length === 8);
|
||||
```
|
||||
|
||||
Your regex should match `"Ohhhhh no"`
|
||||
Your regex should match the string `Ohhhhh no`
|
||||
|
||||
```js
|
||||
assert('Ohhhhh no'.match(ohRegex)[0].length === 9);
|
||||
```
|
||||
|
||||
Your regex should match `"Ohhhhhh no"`
|
||||
Your regex should match the string `Ohhhhhh no`
|
||||
|
||||
```js
|
||||
assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
|
||||
```
|
||||
|
||||
Your regex should not match `"Ohhhhhhh no"`
|
||||
Your regex should not match the string `Ohhhhhhh no`
|
||||
|
||||
```js
|
||||
assert(!ohRegex.test('Ohhhhhhh no'));
|
||||
|
@ -16,19 +16,21 @@ You can search and replace text in a string using `.replace()` on a string. The
|
||||
let wrongText = "The sky is silver.";
|
||||
let silverRegex = /silver/;
|
||||
wrongText.replace(silverRegex, "blue");
|
||||
// Returns "The sky is blue."
|
||||
```
|
||||
|
||||
The `replace` call would return the string `The sky is blue.`.
|
||||
|
||||
You can also access capture groups in the replacement string with dollar signs (`$`).
|
||||
|
||||
```js
|
||||
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
|
||||
// Returns "Camp Code"
|
||||
```
|
||||
|
||||
The `replace` call would return the string `Camp Code`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -38,7 +40,7 @@ You should use `.replace()` to search and replace.
|
||||
assert(code.match(/\.replace\(.*\)/));
|
||||
```
|
||||
|
||||
Your regex should change `"one two three"` to `"three two one"`
|
||||
Your regex should change the string `one two three` to the string `three two one`
|
||||
|
||||
```js
|
||||
assert(result === 'three two one');
|
||||
|
@ -10,7 +10,7 @@ dashedName: using-the-test-method
|
||||
|
||||
Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching.
|
||||
|
||||
If you want to find the word `"the"` in the string `"The dog chased the cat"`, you could use the following regular expression: `/the/`. Notice that quote marks are not required within the regular expression.
|
||||
If you want to find the word `the` in the string `The dog chased the cat`, you could use the following regular expression: `/the/`. Notice that quote marks are not required within the regular expression.
|
||||
|
||||
JavaScript has multiple ways to use regexes. One way to test a regex is using the `.test()` method. 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.
|
||||
|
||||
@ -18,9 +18,10 @@ JavaScript has multiple ways to use regexes. One way to test a regex is using th
|
||||
let testStr = "freeCodeCamp";
|
||||
let testRegex = /Code/;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
The `test` method here returns `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
|
||||
|
Reference in New Issue
Block a user