chore(i8n,learn): processed translations

This commit is contained in:
Crowdin Bot
2021-02-06 04:42:36 +00:00
committed by Mrugesh Mohapatra
parent 15047f2d90
commit e5c44a3ae5
3274 changed files with 172122 additions and 14164 deletions

View File

@ -0,0 +1,75 @@
---
id: 587d7dba367417b2b2512ba8
title: Check for All or None
challengeType: 1
forumTopicId: 301338
dashedName: check-for-all-or-none
---
# --description--
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 for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.
For example, there are slight differences in American and British English and you can use the question mark to match both spellings.
```js
let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true
rainbowRegex.test(british); // Returns true
```
# --instructions--
Change the regex `favRegex` to match both the American English (favorite) and the British English (favourite) version of the word.
# --hints--
Your regex should use the optional symbol, `?`.
```js
favRegex.lastIndex = 0;
assert(favRegex.source.match(/\?/).length > 0);
```
Your regex should match `"favorite"`
```js
favRegex.lastIndex = 0;
assert(favRegex.test('favorite'));
```
Your regex should match `"favourite"`
```js
favRegex.lastIndex = 0;
assert(favRegex.test('favourite'));
```
Your regex should not match `"fav"`
```js
favRegex.lastIndex = 0;
assert(!favRegex.test('fav'));
```
# --seed--
## --seed-contents--
```js
let favWord = "favorite";
let favRegex = /change/; // Change this line
let result = favRegex.test(favWord);
```
# --solutions--
```js
let favWord = "favorite";
let favRegex = /favou?r/;
let result = favRegex.test(favWord);
```

View File

@ -0,0 +1,89 @@
---
id: 5c3dda8b4d8df89bea71600f
title: Check For Mixed Grouping of Characters
challengeType: 1
forumTopicId: 301339
dashedName: check-for-mixed-grouping-of-characters
---
# --description--
Sometimes we want to check for groups of characters using a Regular Expression and to achieve that we use parentheses `()`.
If you want to find either `Penguin` or `Pumpkin` in a string, you can use the following Regular Expression: `/P(engu|umpk)in/g`
Then check whether the desired string groups are in the test string by using the `test()` method.
```js
let testStr = "Pumpkin";
let testRegex = /P(engu|umpk)in/;
testRegex.test(testStr);
// Returns 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.
Then fix the code so that the regex that you have created is checked against `myString` and either `true` or `false` is returned depending on whether the regex matches.
# --hints--
Your regex `myRegex` should return `true` for the string `Franklin D. Roosevelt`
```js
myRegex.lastIndex = 0;
assert(myRegex.test('Franklin D. Roosevelt'));
```
Your regex `myRegex` should return `true` for the string `Eleanor Roosevelt`
```js
myRegex.lastIndex = 0;
assert(myRegex.test('Eleanor Roosevelt'));
```
Your regex `myRegex` should return `false` for the string `Franklin Rosevelt`
```js
myRegex.lastIndex = 0;
assert(!myRegex.test('Franklin Rosevelt'));
```
Your regex `myRegex` should return `false` for the string `Frank Roosevelt`
```js
myRegex.lastIndex = 0;
assert(!myRegex.test('Frank Roosevelt'));
```
You should use `.test()` to test the regex.
```js
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
```
Your result should return `true`.
```js
assert(result === true);
```
# --seed--
## --seed-contents--
```js
let myString = "Eleanor Roosevelt";
let myRegex = /False/; // Change this line
let result = false; // Change this line
// After passing the challenge experiment with myString and see how the grouping works
```
# --solutions--
```js
let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor).*Roosevelt/;
let result = myRegex.test(myString);
```

View File

@ -0,0 +1,73 @@
---
id: 587d7db4367417b2b2512b92
title: Extract Matches
challengeType: 1
forumTopicId: 301340
dashedName: extract-matches
---
# --description--
So far, you have only been checking if a pattern exists or not within a string. You can also extract the actual matches you found with the `.match()` method.
To use the `.match()` method, apply the method on a string and pass in the regex inside the parentheses.
Here's an example:
```js
"Hello, World!".match(/Hello/);
// Returns ["Hello"]
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
// Returns ["expressions"]
```
Note that the `.match` syntax is the "opposite" of the `.test` method you have been using thus far:
```js
'string'.match(/regex/);
/regex/.test('string');
```
# --instructions--
Apply the `.match()` method to extract the word `coding`.
# --hints--
The `result` should have the word `coding`
```js
assert(result.join() === 'coding');
```
Your regex `codingRegex` should search for `coding`
```js
assert(codingRegex.source === 'coding');
```
You should use the `.match()` method.
```js
assert(code.match(/\.match\(.*\)/));
```
# --seed--
## --seed-contents--
```js
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /change/; // Change this line
let result = extractStr; // Change this line
```
# --solutions--
```js
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line
```

View File

@ -0,0 +1,62 @@
---
id: 587d7db6367417b2b2512b9b
title: Find Characters with Lazy Matching
challengeType: 1
forumTopicId: 301341
dashedName: find-characters-with-lazy-matching
---
# --description--
In regular expressions, a <dfn>greedy</dfn> match finds the longest possible part of a string that fits the regex pattern and returns it as a match. The alternative is called a <dfn>lazy</dfn> match, which finds the smallest possible part of the string that satisfies the regex pattern.
You can apply the regex `/t[a-z]*i/` to the string `"titanic"`. This regex is basically a pattern that starts with `t`, ends with `i`, and has some letters in between.
Regular expressions are by default greedy, so the match would return `["titani"]`. It finds the largest sub-string possible to fit the pattern.
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.
# --instructions--
Fix the regex `/<.*>/` to return the HTML tag `<h1>` and not the text `"<h1>Winter is coming</h1>"`. Remember the wildcard `.` in a regular expression matches any character.
# --hints--
The `result` variable should be an array with `<h1>` in it
```js
assert(result[0] == '<h1>');
```
`myRegex` should use lazy matching
```js
assert(/\?/g.test(myRegex));
```
`myRegex` should not include the string 'h1'
```js
assert(!myRegex.source.match('h1'));
```
# --seed--
## --seed-contents--
```js
let text = "<h1>Winter is coming</h1>";
let myRegex = /<.*>/; // Change this line
let result = text.match(myRegex);
```
# --solutions--
```js
let text = "<h1>Winter is coming</h1>";
let myRegex = /<.*?>/; // Change this line
let result = text.match(myRegex);
```

View File

@ -0,0 +1,83 @@
---
id: 587d7db4367417b2b2512b93
title: Find More Than the First Match
challengeType: 1
forumTopicId: 301342
dashedName: find-more-than-the-first-match
---
# --description--
So far, you have only been able to extract or search a pattern once.
```js
let testStr = "Repeat, Repeat, Repeat";
let ourRegex = /Repeat/;
testStr.match(ourRegex);
// Returns ["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"]
```
# --instructions--
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`
# --hints--
Your regex `starRegex` should use the global flag `g`
```js
assert(starRegex.flags.match(/g/).length == 1);
```
Your regex `starRegex` should use the case insensitive flag `i`
```js
assert(starRegex.flags.match(/i/).length == 1);
```
Your match should match both occurrences of the word `"Twinkle"`
```js
assert(
result.sort().join() ==
twinkleStar
.match(/twinkle/gi)
.sort()
.join()
);
```
Your match `result` should have two elements in it.
```js
assert(result.length == 2);
```
# --seed--
## --seed-contents--
```js
let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /change/; // Change this line
let result = twinkleStar; // Change this line
```
# --solutions--
```js
let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /twinkle/gi;
let result = twinkleStar.match(starRegex);
```

View File

@ -0,0 +1,106 @@
---
id: 587d7db7367417b2b2512b9c
title: Find One or More Criminals in a Hunt
challengeType: 1
forumTopicId: 301343
dashedName: find-one-or-more-criminals-in-a-hunt
---
# --description--
Time to pause and test your new regex writing skills. A group of criminals escaped from jail and ran away, but you don't know how many. However, you do know that they stay close together when they are around other people. You are responsible for finding all of the criminals at once.
Here's an example to review how to do this:
The regex `/z+/` matches the letter `z` when it appears one or more times in a row. It would find matches in all of the following strings:
```js
"z"
"zzzzzz"
"ABCzzzz"
"zzzzABC"
"abczzzzzzzzzzzzzzzzzzzzzabc"
```
But it does not find matches in the following strings since there are no letter `z` characters:
```js
""
"ABC"
"abcabc"
```
# --instructions--
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`.
# --hints--
Your regex should match one criminal (`C`) in `"C"`
```js
assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
```
Your regex should match two criminals (`CC`) in `"CC"`
```js
assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
```
Your regex should match three criminals (`CCC`) in `"P1P5P4CCCP2P6P3"`
```js
assert(
'P1P5P4CCCP2P6P3'.match(reCriminals) &&
'P1P5P4CCCP2P6P3'.match(reCriminals)[0] == 'CCC'
);
```
Your regex should match five criminals (`CCCCC`) in `"P6P2P7P4P5CCCCCP3P1"`
```js
assert(
'P6P2P7P4P5CCCCCP3P1'.match(reCriminals) &&
'P6P2P7P4P5CCCCCP3P1'.match(reCriminals)[0] == 'CCCCC'
);
```
Your regex should not match any criminals in `""`
```js
assert(!reCriminals.test(''));
```
Your regex should not match any criminals in `"P1P2P3"`
```js
assert(!reCriminals.test('P1P2P3'));
```
Your regex should match fifty criminals (`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`) in `"P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3"`.
```js
assert(
'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(
reCriminals
) &&
'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(
reCriminals
)[0] == 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'
);
```
# --seed--
## --seed-contents--
```js
let reCriminals = /./; // Change this line
```
# --solutions--
```js
let reCriminals = /C+/; // Change this line
```

View File

@ -0,0 +1,99 @@
---
id: 587d7db4367417b2b2512b91
title: Ignore Case While Matching
challengeType: 1
forumTopicId: 301344
dashedName: ignore-case-while-matching
---
# --description--
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"`.
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.
# --hints--
Your regex should match `freeCodeCamp`
```js
assert(fccRegex.test('freeCodeCamp'));
```
Your regex should match `FreeCodeCamp`
```js
assert(fccRegex.test('FreeCodeCamp'));
```
Your regex should match `FreecodeCamp`
```js
assert(fccRegex.test('FreecodeCamp'));
```
Your regex should match `FreeCodecamp`
```js
assert(fccRegex.test('FreeCodecamp'));
```
Your regex should not match `Free Code Camp`
```js
assert(!fccRegex.test('Free Code Camp'));
```
Your regex should match `FreeCOdeCamp`
```js
assert(fccRegex.test('FreeCOdeCamp'));
```
Your regex should not match `FCC`
```js
assert(!fccRegex.test('FCC'));
```
Your regex should match `FrEeCoDeCamp`
```js
assert(fccRegex.test('FrEeCoDeCamp'));
```
Your regex should match `FrEeCodECamp`
```js
assert(fccRegex.test('FrEeCodECamp'));
```
Your regex should match `FReeCodeCAmp`
```js
assert(fccRegex.test('FReeCodeCAmp'));
```
# --seed--
## --seed-contents--
```js
let myString = "freeCodeCamp";
let fccRegex = /change/; // Change this line
let result = fccRegex.test(myString);
```
# --solutions--
```js
let myString = "freeCodeCamp";
let fccRegex = /freecodecamp/i; // Change this line
let result = fccRegex.test(myString);
```

View File

@ -0,0 +1,83 @@
---
id: 587d7db4367417b2b2512b90
title: Match a Literal String with Different Possibilities
challengeType: 1
forumTopicId: 301345
dashedName: match-a-literal-string-with-different-possibilities
---
# --description--
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/`.
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"`.
# --hints--
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."`
```js
assert(!petRegex.test('Emma has a pet rock.'));
```
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."`
```js
assert(petRegex.test('Liz has a pet cat.'));
```
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."`
```js
assert(petRegex.test('Alice has a pet fish.'));
```
Your regex `petRegex` should return `false` for the string `"Jimmy has a pet computer."`
```js
assert(!petRegex.test('Jimmy has a pet computer.'));
```
# --seed--
## --seed-contents--
```js
let petString = "James has a pet cat.";
let petRegex = /change/; // Change this line
let result = petRegex.test(petString);
```
# --solutions--
```js
let petString = "James has a pet cat.";
let petRegex = /dog|cat|bird|fish/; // Change this line
let result = petRegex.test(petString);
```

View File

@ -0,0 +1,96 @@
---
id: 587d7db7367417b2b2512b9f
title: Match All Letters and Numbers
challengeType: 1
forumTopicId: 301346
dashedName: match-all-letters-and-numbers
---
# --description--
Using character classes, you were able to search for all letters of the alphabet with `[a-z]`. This kind of character class is common enough that there is a shortcut for it, although it includes a few extra characters as well.
The closest character class in JavaScript to match the alphabet is `\w`. This shortcut is equal to `[A-Za-z0-9_]`. This character class matches upper and lowercase letters plus numbers. Note, this character class also includes the underscore character (`_`).
```js
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
```
These shortcut character classes are also known as <dfn>shorthand character classes</dfn>.
# --instructions--
Use the shorthand character class `\w` to count the number of alphanumeric characters in various quotes and strings.
# --hints--
Your regex should use the global flag.
```js
assert(alphabetRegexV2.global);
```
Your regex should use the shorthand character `\w` to match all characters which are alphanumeric.
```js
assert(/\\w/.test(alphabetRegexV2.source));
```
Your regex should find 31 alphanumeric characters in `"The five boxing wizards jump quickly."`
```js
assert(
'The five boxing wizards jump quickly.'.match(alphabetRegexV2).length === 31
);
```
Your regex should find 32 alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
```js
assert(
'Pack my box with five dozen liquor jugs.'.match(alphabetRegexV2).length ===
32
);
```
Your regex should find 30 alphanumeric characters in `"How vexingly quick daft zebras jump!"`
```js
assert(
'How vexingly quick daft zebras jump!'.match(alphabetRegexV2).length === 30
);
```
Your regex should find 36 alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
```js
assert(
'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'.match(alphabetRegexV2)
.length === 36
);
```
# --seed--
## --seed-contents--
```js
let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /change/; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;
```
# --solutions--
```js
let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /\w/g; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;
```

View File

@ -0,0 +1,85 @@
---
id: 587d7db8367417b2b2512ba1
title: Match All Non-Numbers
challengeType: 1
forumTopicId: 301347
dashedName: match-all-non-numbers
---
# --description--
The last challenge showed how to search for digits using the shortcut `\d` with a lowercase `d`. You can also search for non-digits using a similar shortcut that uses an uppercase `D` instead.
The shortcut to look for non-digit characters is `\D`. This is equal to the character class `[^0-9]`, which looks for a single character that is not a number between zero and nine.
# --instructions--
Use the shorthand character class for non-digits `\D` to count how many non-digits are in movie titles.
# --hints--
Your regex should use the shortcut character to match non-digit characters
```js
assert(/\\D/.test(noNumRegex.source));
```
Your regex should use the global flag.
```js
assert(noNumRegex.global);
```
Your regex should find no non-digits in `"9"`.
```js
assert('9'.match(noNumRegex) == null);
```
Your regex should find 6 non-digits in `"Catch 22"`.
```js
assert('Catch 22'.match(noNumRegex).length == 6);
```
Your regex should find 11 non-digits in `"101 Dalmatians"`.
```js
assert('101 Dalmatians'.match(noNumRegex).length == 11);
```
Your regex should find 15 non-digits in `"One, Two, Three"`.
```js
assert('One, Two, Three'.match(noNumRegex).length == 15);
```
Your regex should find 12 non-digits in `"21 Jump Street"`.
```js
assert('21 Jump Street'.match(noNumRegex).length == 12);
```
Your regex should find 17 non-digits in `"2001: A Space Odyssey"`.
```js
assert('2001: A Space Odyssey'.match(noNumRegex).length == 17);
```
# --seed--
## --seed-contents--
```js
let movieName = "2001: A Space Odyssey";
let noNumRegex = /change/; // Change this line
let result = movieName.match(noNumRegex).length;
```
# --solutions--
```js
let movieName = "2001: A Space Odyssey";
let noNumRegex = /\D/g; // Change this line
let result = movieName.match(noNumRegex).length;
```

View File

@ -0,0 +1,85 @@
---
id: 5d712346c441eddfaeb5bdef
title: Match All Numbers
challengeType: 1
forumTopicId: 18181
dashedName: match-all-numbers
---
# --description--
You've learned shortcuts for common string patterns like alphanumerics. Another common pattern is looking for just digits or numbers.
The shortcut to look for digit characters is `\d`, with a lowercase `d`. This is equal to the character class `[0-9]`, which looks for a single character of any number between zero and nine.
# --instructions--
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.
# --hints--
Your regex should use the shortcut character to match digit characters
```js
assert(/\\d/.test(numRegex.source));
```
Your regex should use the global flag.
```js
assert(numRegex.global);
```
Your regex should find 1 digit in `"9"`.
```js
assert('9'.match(numRegex).length == 1);
```
Your regex should find 2 digits in `"Catch 22"`.
```js
assert('Catch 22'.match(numRegex).length == 2);
```
Your regex should find 3 digits in `"101 Dalmatians"`.
```js
assert('101 Dalmatians'.match(numRegex).length == 3);
```
Your regex should find no digits in `"One, Two, Three"`.
```js
assert('One, Two, Three'.match(numRegex) == null);
```
Your regex should find 2 digits in `"21 Jump Street"`.
```js
assert('21 Jump Street'.match(numRegex).length == 2);
```
Your regex should find 4 digits in `"2001: A Space Odyssey"`.
```js
assert('2001: A Space Odyssey'.match(numRegex).length == 4);
```
# --seed--
## --seed-contents--
```js
let movieName = "2001: A Space Odyssey";
let numRegex = /change/; // Change this line
let result = movieName.match(numRegex).length;
```
# --solutions--
```js
let movieName = "2001: A Space Odyssey";
let numRegex = /\d/g; // Change this line
let result = movieName.match(numRegex).length;
```

View File

@ -0,0 +1,117 @@
---
id: 587d7db5367417b2b2512b94
title: Match Anything with Wildcard Period
challengeType: 1
forumTopicId: 301348
dashedName: match-anything-with-wildcard-period
---
# --description--
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.
```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
```
# --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.
# --hints--
You should use the `.test()` method.
```js
assert(code.match(/\.test\(.*\)/));
```
You should use the wildcard character in your regex `unRegex`
```js
assert(/\./.test(unRegex.source));
```
Your regex `unRegex` should match `"run"` in `"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."`
```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."`
```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."`
```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."`
```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."`
```js
unRegex.lastIndex = 0;
assert(
unRegex.test(
'She got fired from the hot dog stand for putting her hair in a bun.'
)
);
```
Your regex `unRegex` should not match `"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."`
```js
unRegex.lastIndex = 0;
assert(!unRegex.test('Catch me if you can.'));
```
# --seed--
## --seed-contents--
```js
let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /change/; // Change this line
let result = unRegex.test(exampleStr);
```
# --solutions--
```js
let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /.un/; // Change this line
let result = unRegex.test(exampleStr);
```

View File

@ -0,0 +1,71 @@
---
id: 587d7db7367417b2b2512b9d
title: Match Beginning String Patterns
challengeType: 1
forumTopicId: 301349
dashedName: match-beginning-string-patterns
---
# --description--
Prior challenges showed that regular expressions can be used to look for a number of matches. They are also used to search for patterns in specific positions in strings.
In an earlier challenge, you used the caret character (`^`) inside a character set to create a negated character set in the form `[^thingsThatWillNotBeMatched]`. Outside of a character set, the caret is used to search for patterns at the beginning of strings.
```js
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
```
# --instructions--
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.
```js
assert(calRegex.source == '^Cal');
```
Your regex should not use any flags.
```js
assert(calRegex.flags == '');
```
Your regex should match `"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.
```js
assert(!calRegex.test('Ricky and Cal both like racing.'));
```
# --seed--
## --seed-contents--
```js
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /change/; // Change this line
let result = calRegex.test(rickyAndCal);
```
# --solutions--
```js
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/; // Change this line
let result = calRegex.test(rickyAndCal);
```

View File

@ -0,0 +1,59 @@
---
id: 587d7db6367417b2b2512b99
title: Match Characters that Occur One or More Times
challengeType: 1
forumTopicId: 301350
dashedName: match-characters-that-occur-one-or-more-times
---
# --description--
Sometimes, you need to match a character (or group of characters) that appears one or more times in a row. This means it occurs at least once, and may be repeated.
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"]`.
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.
# --hints--
Your regex `myRegex` should use the `+` sign to match one or more `s` characters.
```js
assert(/\+/.test(myRegex.source));
```
Your regex `myRegex` should match 2 items.
```js
assert(result.length == 2);
```
The `result` variable should be an array with two matches of `"ss"`
```js
assert(result[0] == 'ss' && result[1] == 'ss');
```
# --seed--
## --seed-contents--
```js
let difficultSpelling = "Mississippi";
let myRegex = /change/; // Change this line
let result = difficultSpelling.match(myRegex);
```
# --solutions--
```js
let difficultSpelling = "Mississippi";
let myRegex = /s+/g; // Change this line
let result = difficultSpelling.match(myRegex);
```

View File

@ -0,0 +1,94 @@
---
id: 587d7db6367417b2b2512b9a
title: Match Characters that Occur Zero or More Times
challengeType: 1
forumTopicId: 301351
dashedName: match-characters-that-occur-zero-or-more-times
---
# --description--
The last challenge used the plus `+` sign to look for characters that occur one or more times. There's also an option that matches characters that occur zero or more times.
The character to do this is the asterisk or star: `*`.
```js
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
```
# --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.
# --hints--
Your regex `chewieRegex` should use the `*` character to match zero or more `a` characters.
```js
assert(/\*/.test(chewieRegex.source));
```
Your regex should match `"A"` in `chewieQuote`.
```js
assert(result[0][0] === 'A');
```
Your regex should match `"Aaaaaaaaaaaaaaaa"` in `chewieQuote`.
```js
assert(result[0] === 'Aaaaaaaaaaaaaaaa');
```
Your regex `chewieRegex` should match 16 characters in `chewieQuote`.
```js
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."
```js
assert(
!"He made a fair move. Screaming about it can't help you.".match(chewieRegex)
);
```
Your regex should not match any characters in "Let him have it. It's not wise to upset a Wookiee."
```js
assert(
!"Let him have it. It's not wise to upset a Wookiee.".match(chewieRegex)
);
```
# --seed--
## --before-user-code--
```js
const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
```
## --seed-contents--
```js
// Only change code below this line
let chewieRegex = /change/; // Change this line
// Only change code above this line
let result = chewieQuote.match(chewieRegex);
```
# --solutions--
```js
let chewieRegex = /Aa*/;
let result = chewieQuote.match(chewieRegex);
```

View File

@ -0,0 +1,66 @@
---
id: 587d7db7367417b2b2512b9e
title: Match Ending String Patterns
challengeType: 1
forumTopicId: 301352
dashedName: match-ending-string-patterns
---
# --description--
In the last challenge, you learned to use the caret character to search for patterns at the beginning of strings. There is also a way to search for patterns at the end of strings.
You can search the end of strings using the dollar sign character `$` at the end of the regex.
```js
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
```
# --instructions--
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.
```js
assert(lastRegex.source == 'caboose$');
```
Your regex should not use any flags.
```js
assert(lastRegex.flags == '');
```
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'));
```
# --seed--
## --seed-contents--
```js
let caboose = "The last car on a train is the caboose";
let lastRegex = /change/; // Change this line
let result = lastRegex.test(caboose);
```
# --solutions--
```js
let caboose = "The last car on a train is the caboose";
let lastRegex = /caboose$/; // Change this line
let result = lastRegex.test(caboose);
```

View File

@ -0,0 +1,90 @@
---
id: 587d7db8367417b2b2512ba0
title: Match Everything But Letters and Numbers
challengeType: 1
forumTopicId: 301353
dashedName: match-everything-but-letters-and-numbers
---
# --description--
You've learned that you can use a shortcut to match alphanumerics `[A-Za-z0-9_]` using `\w`. A natural pattern you might want to search for is the opposite of alphanumerics.
You can search for the opposite of the `\w` with `\W`. Note, the opposite pattern uses a capital letter. This shortcut is the same as `[^A-Za-z0-9_]`.
```js
let shortHand = /\W/;
let numbers = "42%";
let sentence = "Coding!";
numbers.match(shortHand); // Returns ["%"]
sentence.match(shortHand); // Returns ["!"]
```
# --instructions--
Use the shorthand character class `\W` to count the number of non-alphanumeric characters in various quotes and strings.
# --hints--
Your regex should use the global flag.
```js
assert(nonAlphabetRegex.global);
```
Your regex should find 6 non-alphanumeric characters in `"The five boxing wizards jump quickly."`.
```js
assert(
'The five boxing wizards jump quickly.'.match(nonAlphabetRegex).length == 6
);
```
Your regex should use the shorthand character to match characters which are non-alphanumeric.
```js
assert(/\\W/.test(nonAlphabetRegex.source));
```
Your regex should find 8 non-alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
```js
assert(
'Pack my box with five dozen liquor jugs.'.match(nonAlphabetRegex).length == 8
);
```
Your regex should find 6 non-alphanumeric characters in `"How vexingly quick daft zebras jump!"`
```js
assert(
'How vexingly quick daft zebras jump!'.match(nonAlphabetRegex).length == 6
);
```
Your regex should find 12 non-alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
```js
assert(
'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'.match(nonAlphabetRegex)
.length == 12
);
```
# --seed--
## --seed-contents--
```js
let quoteSample = "The five boxing wizards jump quickly.";
let nonAlphabetRegex = /change/; // Change this line
let result = quoteSample.match(nonAlphabetRegex).length;
```
# --solutions--
```js
let quoteSample = "The five boxing wizards_jump quickly.";
let nonAlphabetRegex = /\W/g; // Change this line
let result = quoteSample.match(nonAlphabetRegex).length;
```

View File

@ -0,0 +1,69 @@
---
id: 587d7db5367417b2b2512b96
title: Match Letters of the Alphabet
challengeType: 1
forumTopicId: 301354
dashedName: match-letters-of-the-alphabet
---
# --description--
You saw how you can use <dfn>character sets</dfn> to specify a group of characters to match, but that's a lot of typing when you need to match a large range of characters (for example, every letter in the alphabet). Fortunately, there is a built-in feature that makes this short and simple.
Inside a character set, you can define a range of characters to match using a hyphen character: `-`.
For example, to match lowercase letters `a` through `e` you would use `[a-e]`.
```js
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
```
# --instructions--
Match all the letters in the string `quoteSample`.
**Note**: Be sure to match both uppercase and lowercase letters.
# --hints--
Your regex `alphabetRegex` should match 35 items.
```js
assert(result.length == 35);
```
Your regex `alphabetRegex` should use the global flag.
```js
assert(alphabetRegex.flags.match(/g/).length == 1);
```
Your regex `alphabetRegex` should use the case insensitive flag.
```js
assert(alphabetRegex.flags.match(/i/).length == 1);
```
# --seed--
## --seed-contents--
```js
let quoteSample = "The quick brown fox jumps over the lazy dog.";
let alphabetRegex = /change/; // Change this line
let result = alphabetRegex; // Change this line
```
# --solutions--
```js
let quoteSample = "The quick brown fox jumps over the lazy dog.";
let alphabetRegex = /[a-z]/gi; // Change this line
let result = quoteSample.match(alphabetRegex); // Change this line
```

View File

@ -0,0 +1,70 @@
---
id: 587d7db3367417b2b2512b8f
title: Match Literal Strings
challengeType: 1
forumTopicId: 301355
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"`:
```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"`.
```js
let wrongRegex = /kevin/;
wrongRegex.test(testStr);
// Returns false
```
A future challenge will show how to match those other forms as well.
# --instructions--
Complete the regex `waldoRegex` to find `"Waldo"` in the string `waldoIsHiding` with a literal match.
# --hints--
Your regex `waldoRegex` should find `"Waldo"`
```js
assert(waldoRegex.test(waldoIsHiding));
```
Your regex `waldoRegex` should not search for anything else.
```js
assert(!waldoRegex.test('Somewhere is hiding in this text.'));
```
You should perform a literal string match with your regex.
```js
assert(!/\/.*\/i/.test(code));
```
# --seed--
## --seed-contents--
```js
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
let waldoRegex = /search/; // Change this line
let result = waldoRegex.test(waldoIsHiding);
```
# --solutions--
```js
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
let waldoRegex = /Waldo/; // Change this line
let result = waldoRegex.test(waldoIsHiding);
```

View File

@ -0,0 +1,76 @@
---
id: 587d7db9367417b2b2512ba4
title: Match Non-Whitespace Characters
challengeType: 1
forumTopicId: 18210
dashedName: match-non-whitespace-characters
---
# --description--
You learned about searching for whitespace using `\s`, with a lowercase `s`. You can also search for everything except whitespace.
Search for non-whitespace using `\S`, which is an uppercase `s`. This pattern will not match whitespace, carriage return, tab, form feed, and new line characters. You can think of it being similar to the character class `[^ \r\t\f\n\v]`.
```js
let whiteSpace = "Whitespace. Whitespace everywhere!"
let nonSpaceRegex = /\S/g;
whiteSpace.match(nonSpaceRegex).length; // Returns 32
```
# --instructions--
Change the regex `countNonWhiteSpace` to look for multiple non-whitespace characters in a string.
# --hints--
Your regex should use the global flag.
```js
assert(countNonWhiteSpace.global);
```
Your regex should use the shorthand character `\S` to match all non-whitespace characters.
```js
assert(/\\S/.test(countNonWhiteSpace.source));
```
Your regex should find 35 non-spaces in `"Men are from Mars and women are from Venus."`
```js
assert(
'Men are from Mars and women are from Venus.'.match(countNonWhiteSpace)
.length == 35
);
```
Your regex should find 23 non-spaces in `"Space: the final frontier."`
```js
assert('Space: the final frontier.'.match(countNonWhiteSpace).length == 23);
```
Your regex should find 21 non-spaces in `"MindYourPersonalSpace"`
```js
assert('MindYourPersonalSpace'.match(countNonWhiteSpace).length == 21);
```
# --seed--
## --seed-contents--
```js
let sample = "Whitespace is important in separating words";
let countNonWhiteSpace = /change/; // Change this line
let result = sample.match(countNonWhiteSpace);
```
# --solutions--
```js
let sample = "Whitespace is important in separating words";
let countNonWhiteSpace = /\S/g; // Change this line
let result = sample.match(countNonWhiteSpace);
```

View File

@ -0,0 +1,64 @@
---
id: 587d7db5367417b2b2512b97
title: Match Numbers and Letters of the Alphabet
challengeType: 1
forumTopicId: 301356
dashedName: match-numbers-and-letters-of-the-alphabet
---
# --description--
Using the hyphen (`-`) to match a range of characters is not limited to letters. It also works to match a range of numbers.
For example, `/[0-5]/` matches any number between `0` and `5`, including the `0` and `5`.
Also, it is possible to combine a range of letters and numbers in a single character set.
```js
let jennyStr = "Jenny8675309";
let myRegex = /[a-z0-9]/ig;
// matches all letters and numbers in jennyStr
jennyStr.match(myRegex);
```
# --instructions--
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.
# --hints--
Your regex `myRegex` should match 17 items.
```js
assert(result.length == 17);
```
Your regex `myRegex` should use the global flag.
```js
assert(myRegex.flags.match(/g/).length == 1);
```
Your regex `myRegex` should use the case insensitive flag.
```js
assert(myRegex.flags.match(/i/).length == 1);
```
# --seed--
## --seed-contents--
```js
let quoteSample = "Blueberry 3.141592653s are delicious.";
let myRegex = /change/; // Change this line
let result = myRegex; // Change this line
```
# --solutions--
```js
let quoteSample = "Blueberry 3.141592653s are delicious.";
let myRegex = /[h-s2-6]/gi; // Change this line
let result = quoteSample.match(myRegex); // Change this line
```

View File

@ -0,0 +1,84 @@
---
id: 587d7db5367417b2b2512b95
title: Match Single Character with Multiple Possibilities
challengeType: 1
forumTopicId: 301357
dashedName: match-single-character-with-multiple-possibilities
---
# --description--
You learned how to match literal patterns (`/literal/`) and wildcard character (`/./`). Those are the extremes of regular expressions, where one finds exact matches and the other matches everything. There are options that are a balance between the two extremes.
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"`.
```js
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
```
# --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.
# --hints--
You should find all 25 vowels.
```js
assert(result.length == 25);
```
Your regex `vowelRegex` should use a character class.
```js
assert(/\[.*\]/.test(vowelRegex.source));
```
Your regex `vowelRegex` should use the global flag.
```js
assert(vowelRegex.flags.match(/g/).length == 1);
```
Your regex `vowelRegex` should use the case insensitive flag.
```js
assert(vowelRegex.flags.match(/i/).length == 1);
```
Your regex should not match any consonants.
```js
assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));
```
# --seed--
## --seed-contents--
```js
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /change/; // Change this line
let result = vowelRegex; // Change this line
```
# --solutions--
```js
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
```

View File

@ -0,0 +1,57 @@
---
id: 587d7db6367417b2b2512b98
title: Match Single Characters Not Specified
challengeType: 1
forumTopicId: 301358
dashedName: match-single-characters-not-specified
---
# --description--
So far, you have created a set of characters that you want to match, but you could also create a set of characters that you do not want to match. These types of character sets are called <dfn>negated character sets</dfn>.
To create a negated character set, you place a caret character (`^`) after the opening bracket and before the characters you do not want to match.
For example, `/[^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.
# --instructions--
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.
# --hints--
Your regex `myRegex` should match 9 items.
```js
assert(result.length == 9);
```
Your regex `myRegex` should use the global flag.
```js
assert(myRegex.flags.match(/g/).length == 1);
```
Your regex `myRegex` should use the case insensitive flag.
```js
assert(myRegex.flags.match(/i/).length == 1);
```
# --seed--
## --seed-contents--
```js
let quoteSample = "3 blind mice.";
let myRegex = /change/; // Change this line
let result = myRegex; // Change this line
```
# --solutions--
```js
let quoteSample = "3 blind mice.";
let myRegex = /[^0-9aeiou]/gi; // Change this line
let result = quoteSample.match(myRegex); // Change this line
```

View File

@ -0,0 +1,77 @@
---
id: 587d7db8367417b2b2512ba3
title: Match Whitespace
challengeType: 1
forumTopicId: 301359
dashedName: match-whitespace
---
# --description--
The challenges so far have covered matching letters of the alphabet and numbers. You can also match the whitespace or spaces between letters.
You can search for whitespace using `\s`, which is a lowercase `s`. This pattern not only matches whitespace, but also carriage return, tab, form feed, and new line characters. You can think of it as similar to the character class `[ \r\t\f\n\v]`.
```js
let whiteSpace = "Whitespace. Whitespace everywhere!"
let spaceRegex = /\s/g;
whiteSpace.match(spaceRegex);
// Returns [" ", " "]
```
# --instructions--
Change the regex `countWhiteSpace` to look for multiple whitespace characters in a string.
# --hints--
Your regex should use the global flag.
```js
assert(countWhiteSpace.global);
```
Your regex should use the shorthand character `\s` to match all whitespace characters.
```js
assert(/\\s/.test(countWhiteSpace.source));
```
Your regex should find eight spaces in `"Men are from Mars and women are from Venus."`
```js
assert(
'Men are from Mars and women are from Venus.'.match(countWhiteSpace).length ==
8
);
```
Your regex should find three spaces in `"Space: the final frontier."`
```js
assert('Space: the final frontier.'.match(countWhiteSpace).length == 3);
```
Your regex should find no spaces in `"MindYourPersonalSpace"`
```js
assert('MindYourPersonalSpace'.match(countWhiteSpace) == null);
```
# --seed--
## --seed-contents--
```js
let sample = "Whitespace is important in separating words";
let countWhiteSpace = /change/; // Change this line
let result = sample.match(countWhiteSpace);
```
# --solutions--
```js
let sample = "Whitespace is important in separating words";
let countWhiteSpace = /\s/g;
let result = sample.match(countWhiteSpace);
```

View File

@ -0,0 +1,112 @@
---
id: 587d7dba367417b2b2512ba9
title: Positive and Negative Lookahead
challengeType: 1
forumTopicId: 301360
dashedName: positive-and-negative-lookahead
---
# --description--
<dfn>Lookaheads</dfn> 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: <dfn>positive lookahead</dfn> and <dfn>negative lookahead</dfn>.
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.
On the other hand, 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.
Lookaheads are a bit confusing but some examples will help.
```js
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["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
```
# --instructions--
Use lookaheads in the `pwRegex` to match passwords that are greater than 5 characters long, and have two consecutive digits.
# --hints--
Your regex should use two positive `lookaheads`.
```js
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
```
Your regex should not match `"astronaut"`
```js
assert(!pwRegex.test('astronaut'));
```
Your regex should not match `"banan1"`
```js
assert(!pwRegex.test('banan1'));
```
Your regex should match `"bana12"`
```js
assert(pwRegex.test('bana12'));
```
Your regex should match `"abc123"`
```js
assert(pwRegex.test('abc123'));
```
Your regex should not match `"12345"`
```js
assert(!pwRegex.test('12345'));
```
Your regex should match `"8pass99"`
```js
assert(pwRegex.test('8pass99'));
```
Your regex should not match `"1a2bcde"`
```js
assert(!pwRegex.test('1a2bcde'));
```
Your regex should match `"astr1on11aut"`
```js
assert(pwRegex.test('astr1on11aut'));
```
# --seed--
## --seed-contents--
```js
let sampleWord = "astronaut";
let pwRegex = /change/; // Change this line
let result = pwRegex.test(sampleWord);
```
# --solutions--
```js
let pwRegex = /(?=\w{6})(?=\w*\d{2})/;
```

View File

@ -0,0 +1,55 @@
---
id: 587d7dbb367417b2b2512bac
title: Remove Whitespace from Start and End
challengeType: 1
forumTopicId: 301362
dashedName: remove-whitespace-from-start-and-end
---
# --description--
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.
# --instructions--
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.
# --hints--
`result` should equal to `"Hello, World!"`
```js
assert(result == 'Hello, World!');
```
Your solution should not use the `String.prototype.trim()` method.
```js
assert(!code.match(/\.?[\s\S]*?trim/));
```
The `result` variable should not be set equal to a string.
```js
assert(!code.match(/result\s*=\s*".*?"/));
```
# --seed--
## --seed-contents--
```js
let hello = " Hello, World! ";
let wsRegex = /change/; // Change this line
let result = hello; // Change this line
```
# --solutions--
```js
let hello = " Hello, World! ";
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
let result = hello.replace(wsRegex, '$2');
```

View File

@ -0,0 +1,117 @@
---
id: 587d7db8367417b2b2512ba2
title: Restrict Possible Usernames
challengeType: 1
forumTopicId: 301363
dashedName: restrict-possible-usernames
---
# --description--
Usernames are used everywhere on the internet. They are what give users a unique identity on their favorite sites.
You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username.
1) Usernames can only use alpha-numeric characters.
2) The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
3) Username letters can be lowercase and uppercase.
4) Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
# --instructions--
Change the regex `userCheck` to fit the constraints listed above.
# --hints--
Your regex should match `JACK`
```js
assert(userCheck.test('JACK'));
```
Your regex should not match `J`
```js
assert(!userCheck.test('J'));
```
Your regex should match `Jo`
```js
assert(userCheck.test('Jo'));
```
Your regex should match `Oceans11`
```js
assert(userCheck.test('Oceans11'));
```
Your regex should match `RegexGuru`
```js
assert(userCheck.test('RegexGuru'));
```
Your regex should not match `007`
```js
assert(!userCheck.test('007'));
```
Your regex should not match `9`
```js
assert(!userCheck.test('9'));
```
Your regex should not match `A1`
```js
assert(!userCheck.test('A1'));
```
Your regex should not match `BadUs3rnam3`
```js
assert(!userCheck.test('BadUs3rnam3'));
```
Your regex should match `Z97`
```js
assert(userCheck.test('Z97'));
```
Your regex should not match `c57bT3`
```js
assert(!userCheck.test('c57bT3'));
```
Your regex should match `AB1`
```js
assert(userCheck.test('AB1'));
```
# --seed--
## --seed-contents--
```js
let username = "JackOfAllTrades";
let userCheck = /change/; // Change this line
let result = userCheck.test(username);
```
# --solutions--
```js
let username = "JackOfAllTrades";
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
let result = userCheck.test(username);
```

View File

@ -0,0 +1,104 @@
---
id: 587d7dbb367417b2b2512baa
title: Reuse Patterns Using Capture Groups
challengeType: 1
forumTopicId: 301364
dashedName: reuse-patterns-using-capture-groups
---
# --description--
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 <dfn>capture groups</dfn>. Parentheses, `(` and `)`, are used to find repeat substrings. You put the regex of the pattern that will repeat in between the parentheses.
To specify where that repeat string will appear, you use a backslash (<code>\\</code>) and then a 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.
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"]
```
Using the `.match()` method on a string will return an array with the string it matches, along with its capture group.
# --instructions--
Use capture groups in `reRegex` to match a string that consists of only the same number repeated exactly three times separated by single spaces.
# --hints--
Your regex should use the shorthand character class for digits.
```js
assert(reRegex.source.match(/\\d/));
```
Your regex should reuse a capture group twice.
```js
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
```
Your regex should match `"42 42 42"`.
```js
assert(reRegex.test('42 42 42'));
```
Your regex should match `"100 100 100"`.
```js
assert(reRegex.test('100 100 100'));
```
Your regex should not match `"42 42 42 42"`.
```js
assert.equal('42 42 42 42'.match(reRegex.source), null);
```
Your regex should not match `"42 42"`.
```js
assert.equal('42 42'.match(reRegex.source), null);
```
Your regex should not match `"101 102 103"`.
```js
assert(!reRegex.test('101 102 103'));
```
Your regex should not match `"1 2 3"`.
```js
assert(!reRegex.test('1 2 3'));
```
Your regex should match `"10 10 10"`.
```js
assert(reRegex.test('10 10 10'));
```
# --seed--
## --seed-contents--
```js
let repeatNum = "42 42 42";
let reRegex = /change/; // Change this line
let result = reRegex.test(repeatNum);
```
# --solutions--
```js
let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/;
let result = reRegex.test(repeatNum);
```

View File

@ -0,0 +1,90 @@
---
id: 587d7db9367417b2b2512ba7
title: Specify Exact Number of Matches
challengeType: 1
forumTopicId: 301365
dashedName: specify-exact-number-of-matches
---
# --description--
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.
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
```
# --instructions--
Change the regex `timRegex` to match the word `"Timber"` only when it has four letter `m`'s.
# --hints--
Your regex should use curly brackets.
```js
assert(timRegex.source.match(/{.*?}/).length > 0);
```
Your regex should not match `"Timber"`
```js
timRegex.lastIndex = 0;
assert(!timRegex.test('Timber'));
```
Your regex should not match `"Timmber"`
```js
timRegex.lastIndex = 0;
assert(!timRegex.test('Timmber'));
```
Your regex should not match `"Timmmber"`
```js
timRegex.lastIndex = 0;
assert(!timRegex.test('Timmmber'));
```
Your regex should match `"Timmmmber"`
```js
timRegex.lastIndex = 0;
assert(timRegex.test('Timmmmber'));
```
Your regex should not match `"Timber"` with 30 `m`'s in it.
```js
timRegex.lastIndex = 0;
assert(!timRegex.test('Ti' + 'm'.repeat(30) + 'ber'));
```
# --seed--
## --seed-contents--
```js
let timStr = "Timmmmber";
let timRegex = /change/; // Change this line
let result = timRegex.test(timStr);
```
# --solutions--
```js
let timStr = "Timmmmber";
let timRegex = /Tim{4}ber/; // Change this line
let result = timRegex.test(timStr);
```

View File

@ -0,0 +1,91 @@
---
id: 587d7db9367417b2b2512ba6
title: Specify Only the Lower Number of Matches
challengeType: 1
forumTopicId: 301366
dashedName: specify-only-the-lower-number-of-matches
---
# --description--
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.
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
```
# --instructions--
Change the regex `haRegex` to match the word `"Hazzah"` only when it has four or more letter `z`'s.
# --hints--
Your regex should use curly brackets.
```js
assert(haRegex.source.match(/{.*?}/).length > 0);
```
Your regex should not match `"Hazzah"`
```js
assert(!haRegex.test('Hazzah'));
```
Your regex should not match `"Hazzzah"`
```js
assert(!haRegex.test('Hazzzah'));
```
Your regex should match `"Hazzzzah"`
```js
assert('Hazzzzah'.match(haRegex)[0].length === 8);
```
Your regex should match `"Hazzzzzah"`
```js
assert('Hazzzzzah'.match(haRegex)[0].length === 9);
```
Your regex should match `"Hazzzzzzah"`
```js
assert('Hazzzzzzah'.match(haRegex)[0].length === 10);
```
Your regex should match `"Hazzah"` with 30 `z`'s in it.
```js
assert('Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah'.match(haRegex)[0].length === 34);
```
# --seed--
## --seed-contents--
```js
let haStr = "Hazzzzah";
let haRegex = /change/; // Change this line
let result = haRegex.test(haStr);
```
# --solutions--
```js
let haStr = "Hazzzzah";
let haRegex = /Haz{4,}ah/; // Change this line
let result = haRegex.test(haStr);
```

View File

@ -0,0 +1,89 @@
---
id: 587d7db9367417b2b2512ba5
title: Specify Upper and Lower Number of Matches
challengeType: 1
forumTopicId: 301367
dashedName: specify-upper-and-lower-number-of-matches
---
# --description--
Recall that you use the plus sign `+` to look for one or more characters and the asterisk `*` to look for zero or more characters. These are convenient but sometimes you want to match a certain range of patterns.
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/`.
```js
let A4 = "aaaah";
let A2 = "aah";
let multipleA = /a{3,5}h/;
multipleA.test(A4); // Returns true
multipleA.test(A2); // Returns false
```
# --instructions--
Change the regex `ohRegex` to match the entire phrase `"Oh no"` only when it has `3` to `6` letter `h`'s.
# --hints--
Your regex should use curly brackets.
```js
assert(ohRegex.source.match(/{.*?}/).length > 0);
```
Your regex should not match `"Ohh no"`
```js
assert(!ohRegex.test('Ohh no'));
```
Your regex should match `"Ohhh no"`
```js
assert('Ohhh no'.match(ohRegex)[0].length === 7);
```
Your regex should match `"Ohhhh no"`
```js
assert('Ohhhh no'.match(ohRegex)[0].length === 8);
```
Your regex should match `"Ohhhhh no"`
```js
assert('Ohhhhh no'.match(ohRegex)[0].length === 9);
```
Your regex should match `"Ohhhhhh no"`
```js
assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
```
Your regex should not match `"Ohhhhhhh no"`
```js
assert(!ohRegex.test('Ohhhhhhh no'));
```
# --seed--
## --seed-contents--
```js
let ohStr = "Ohhh no";
let ohRegex = /change/; // Change this line
let result = ohRegex.test(ohStr);
```
# --solutions--
```js
let ohStr = "Ohhh no";
let ohRegex = /Oh{3,6} no/; // Change this line
let result = ohRegex.test(ohStr);
```

View File

@ -0,0 +1,86 @@
---
id: 587d7dbb367417b2b2512bab
title: Use Capture Groups to Search and Replace
challengeType: 1
forumTopicId: 301368
dashedName: use-capture-groups-to-search-and-replace
---
# --description--
Searching is useful. However, you can make searching even more powerful when it also changes (or replaces) the text you match.
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.
```js
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "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"
```
# --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.
# --hints--
You should use `.replace()` to search and replace.
```js
assert(code.match(/\.replace\(.*\)/));
```
Your regex should change `"one two three"` to `"three two one"`
```js
assert(result === 'three two one');
```
You should not change the last line.
```js
assert(code.match(/result\s*=\s*str\.replace\(.*?\)/));
```
`fixRegex` should use at least three capture groups.
```js
assert(new RegExp(fixRegex.source + '|').exec('').length - 1 >= 3);
```
`replaceText` should use parenthesized submatch string(s) (i.e. the nth parenthesized submatch string, $n, corresponds to the nth capture group).
```js
{
const re = /(\$\d{1,2})+(?:[\D]|\b)/g;
assert(replaceText.match(re).length >= 3);
}
```
# --seed--
## --seed-contents--
```js
let str = "one two three";
let fixRegex = /change/; // Change this line
let replaceText = ""; // Change this line
let result = str.replace(fixRegex, replaceText);
```
# --solutions--
```js
let str = "one two three";
let fixRegex = /(\w+) (\w+) (\w+)/g; // Change this line
let replaceText = "$3 $2 $1"; // Change this line
let result = str.replace(fixRegex, replaceText);
```

View File

@ -0,0 +1,58 @@
---
id: 587d7db3367417b2b2512b8e
title: Using the Test Method
challengeType: 1
forumTopicId: 301369
dashedName: using-the-test-method
---
# --description--
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.
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.
```js
let testStr = "freeCodeCamp";
let testRegex = /Code/;
testRegex.test(testStr);
// Returns true
```
# --instructions--
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
# --hints--
You should use `.test()` to test the regex.
```js
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
```
Your result should return `true`.
```js
assert(result === true);
```
# --seed--
## --seed-contents--
```js
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex; // Change this line
```
# --solutions--
```js
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // Change this line
```