Feat: add new Markdown parser (#39800)
and change all the challenges to new `md` format.
This commit is contained in:
committed by
GitHub
parent
a07f84c8ec
commit
0bd52f8bd1
@ -5,10 +5,12 @@ challengeType: 1
|
||||
forumTopicId: 301338
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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, <code>?</code>. This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.
|
||||
|
||||
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
|
||||
@ -19,35 +21,43 @@ rainbowRegex.test(american); // Returns true
|
||||
rainbowRegex.test(british); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the regex <code>favRegex</code> to match both the American English (favorite) and the British English (favourite) version of the word.
|
||||
</section>
|
||||
Change the regex `favRegex` to match both the American English (favorite) and the British English (favourite) version of the word.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the optional symbol, <code>?</code>.
|
||||
testString: favRegex.lastIndex = 0; assert(favRegex.source.match(/\?/).length > 0);
|
||||
- text: Your regex should match <code>"favorite"</code>
|
||||
testString: favRegex.lastIndex = 0; assert(favRegex.test("favorite"));
|
||||
- text: Your regex should match <code>"favourite"</code>
|
||||
testString: favRegex.lastIndex = 0; assert(favRegex.test("favourite"));
|
||||
- text: Your regex should not match <code>"fav"</code>
|
||||
testString: favRegex.lastIndex = 0; assert(!favRegex.test("fav"));
|
||||
Your regex should use the optional symbol, `?`.
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(favRegex.source.match(/\?/).length > 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should match `"favorite"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(favRegex.test('favorite'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -55,19 +65,10 @@ let favRegex = /change/; // Change this line
|
||||
let result = favRegex.test(favWord);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let favWord = "favorite";
|
||||
let favRegex = /favou?r/;
|
||||
let result = favRegex.test(favWord);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301339
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Sometimes we want to check for groups of characters using a Regular Expression and to achieve that we use parentheses <code>()</code>.
|
||||
If you want to find either <code>Penguin</code> or <code>Pumpkin</code> in a string, you can use the following Regular Expression: <code>/P(engu|umpk)in/g</code>
|
||||
Then check whether the desired string groups are in the test string by using the <code>test()</code> method.
|
||||
# --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";
|
||||
@ -18,39 +20,57 @@ testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Fix the regex so that it checks for the names of <code>Franklin Roosevelt</code> or <code>Eleanor Roosevelt</code> 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 <code>myString</code> and either <code>true</code> or <code>false</code> is returned depending on whether the regex matches.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
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.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>myRegex</code> should return <code>true</code> for the string <code>Franklin D. Roosevelt</code>
|
||||
testString: myRegex.lastIndex = 0; assert(myRegex.test('Franklin D. Roosevelt'));
|
||||
- text: Your regex <code>myRegex</code> should return <code>true</code> for the string <code>Eleanor Roosevelt</code>
|
||||
testString: myRegex.lastIndex = 0; assert(myRegex.test('Eleanor Roosevelt'));
|
||||
- text: Your regex <code>myRegex</code> should return <code>false</code> for the string <code>Franklin Rosevelt</code>
|
||||
testString: myRegex.lastIndex = 0; assert(!myRegex.test('Franklin Rosevelt'));
|
||||
- text: Your regex <code>myRegex</code> should return <code>false</code> for the string <code>Frank Roosevelt</code>
|
||||
testString: myRegex.lastIndex = 0; assert(!myRegex.test('Frank Roosevelt'));
|
||||
- text: You should use <code>.test()</code> to test the regex.
|
||||
testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
- text: Your result should return <code>true</code>.
|
||||
testString: assert(result === true);
|
||||
# --hints--
|
||||
|
||||
Your regex `myRegex` should return `true` for the string `Franklin D. Roosevelt`
|
||||
|
||||
```js
|
||||
myRegex.lastIndex = 0;
|
||||
assert(myRegex.test('Franklin D. Roosevelt'));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `myRegex` should return `true` for the string `Eleanor Roosevelt`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
myRegex.lastIndex = 0;
|
||||
assert(myRegex.test('Eleanor Roosevelt'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -59,19 +79,10 @@ let result = false; // Change this line
|
||||
// After passing the challenge experiment with myString and see how the grouping works
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myString = "Eleanor Roosevelt";
|
||||
let myRegex = /(Franklin|Eleanor).*Roosevelt/;
|
||||
let result = myRegex.test(myString);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,12 @@ challengeType: 1
|
||||
forumTopicId: 301340
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='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 <code>.match()</code> method.
|
||||
To use the <code>.match()</code> method, apply the method on a string and pass in the regex inside the parentheses.
|
||||
# --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
|
||||
@ -27,33 +29,33 @@ Note that the `.match` syntax is the "opposite" of the `.test` method you have b
|
||||
/regex/.test('string');
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Apply the <code>.match()</code> method to extract the word <code>coding</code>.
|
||||
</section>
|
||||
Apply the `.match()` method to extract the word `coding`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The <code>result</code> should have the word <code>coding</code>
|
||||
testString: assert(result.join() === "coding");
|
||||
- text: Your regex <code>codingRegex</code> should search for <code>coding</code>
|
||||
testString: assert(codingRegex.source === "coding");
|
||||
- text: You should use the <code>.match()</code> method.
|
||||
testString: assert(code.match(/\.match\(.*\)/));
|
||||
The `result` should have the word `coding`
|
||||
|
||||
```js
|
||||
assert(result.join() === 'coding');
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `codingRegex` should search for `coding`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(codingRegex.source === 'coding');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should use the `.match()` method.
|
||||
|
||||
```js
|
||||
assert(code.match(/\.match\(.*\)/));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let extractStr = "Extract the word 'coding' from this string.";
|
||||
@ -61,19 +63,10 @@ let codingRegex = /change/; // Change this line
|
||||
let result = extractStr; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --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
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,40 +5,46 @@ challengeType: 1
|
||||
forumTopicId: 301341
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>/t[a-z]*i/</code> to the string <code>"titanic"</code>. This regex is basically a pattern that starts with <code>t</code>, ends with <code>i</code>, and has some letters in between.
|
||||
Regular expressions are by default greedy, so the match would return <code>["titani"]</code>. It finds the largest sub-string possible to fit the pattern.
|
||||
However, you can use the <code>?</code> character to change it to lazy matching. <code>"titanic"</code> matched against the adjusted regex of <code>/t[a-z]*?i/</code> returns <code>["ti"]</code>.
|
||||
<strong>Note</strong><br>Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Fix the regex <code>/<.*>/</code> to return the HTML tag <code><h1></code> and not the text <code>"<h1>Winter is coming</h1>"</code>. Remember the wildcard <code>.</code> in a regular expression matches any character.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Regular expressions are by default greedy, so the match would return `["titani"]`. It finds the largest sub-string possible to fit the pattern.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The <code>result</code> variable should be an array with <code><h1></code> in it
|
||||
testString: assert(result[0] == '<h1>');
|
||||
- text: <code>myRegex</code> should use lazy matching
|
||||
testString: assert(/\?/g.test(myRegex));
|
||||
- text: <code>myRegex</code> should not include the string 'h1'
|
||||
testString: assert(!myRegex.source.match('h1'));
|
||||
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>');
|
||||
```
|
||||
|
||||
</section>
|
||||
`myRegex` should use lazy matching
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/\?/g.test(myRegex));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`myRegex` should not include the string 'h1'
|
||||
|
||||
```js
|
||||
assert(!myRegex.source.match('h1'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let text = "<h1>Winter is coming</h1>";
|
||||
@ -46,19 +52,10 @@ let myRegex = /<.*>/; // Change this line
|
||||
let result = text.match(myRegex);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let text = "<h1>Winter is coming</h1>";
|
||||
let myRegex = /<.*?>/; // Change this line
|
||||
let result = text.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,8 +5,8 @@ challengeType: 1
|
||||
forumTopicId: 301342
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
So far, you have only been able to extract or search a pattern once.
|
||||
|
||||
```js
|
||||
@ -16,7 +16,7 @@ testStr.match(ourRegex);
|
||||
// Returns ["Repeat"]
|
||||
```
|
||||
|
||||
To search or extract a pattern more than once, you can use the <code>g</code> flag.
|
||||
To search or extract a pattern more than once, you can use the `g` flag.
|
||||
|
||||
```js
|
||||
let repeatRegex = /Repeat/g;
|
||||
@ -24,36 +24,48 @@ testStr.match(repeatRegex);
|
||||
// Returns ["Repeat", "Repeat", "Repeat"]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Using the regex <code>starRegex</code>, find and extract both <code>"Twinkle"</code> words from the string <code>twinkleStar</code>.
|
||||
<strong>Note</strong><br>You can have multiple flags on your regex like <code>/search/gi</code>
|
||||
</section>
|
||||
Using the regex `starRegex`, find and extract both `"Twinkle"` words from the string `twinkleStar`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note**
|
||||
You can have multiple flags on your regex like `/search/gi`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>starRegex</code> should use the global flag <code>g</code>
|
||||
testString: assert(starRegex.flags.match(/g/).length == 1);
|
||||
- text: Your regex <code>starRegex</code> should use the case insensitive flag <code>i</code>
|
||||
testString: assert(starRegex.flags.match(/i/).length == 1);
|
||||
- text: Your match should match both occurrences of the word <code>"Twinkle"</code>
|
||||
testString: assert(result.sort().join() == twinkleStar.match(/twinkle/gi).sort().join());
|
||||
- text: Your match <code>result</code> should have two elements in it.
|
||||
testString: assert(result.length == 2);
|
||||
# --hints--
|
||||
|
||||
Your regex `starRegex` should use the global flag `g`
|
||||
|
||||
```js
|
||||
assert(starRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `starRegex` should use the case insensitive flag `i`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(starRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -61,19 +73,10 @@ let starRegex = /change/; // Change this line
|
||||
let result = twinkleStar; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let twinkleStar = "Twinkle, twinkle, little star";
|
||||
let starRegex = /twinkle/gi;
|
||||
let result = twinkleStar.match(starRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301343
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>/z+/</code> matches the letter <code>z</code> when it appears one or more times in a row. It would find matches in all of the following strings:
|
||||
|
||||
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"
|
||||
@ -19,7 +21,7 @@ The regex <code>/z+/</code> matches the letter <code>z</code> when it appears on
|
||||
"abczzzzzzzzzzzzzzzzzzzzzabc"
|
||||
```
|
||||
|
||||
But it does not find matches in the following strings since there are no letter <code>z</code> characters:
|
||||
But it does not find matches in the following strings since there are no letter `z` characters:
|
||||
|
||||
```js
|
||||
""
|
||||
@ -27,58 +29,77 @@ But it does not find matches in the following strings since there are no letter
|
||||
"abcabc"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='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 <code>C</code>.
|
||||
</section>
|
||||
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`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should match one criminal (<code>C</code>) in <code>"C"</code>
|
||||
testString: assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
|
||||
- text: Your regex should match two criminals (<code>CC</code>) in <code>"CC"</code>
|
||||
testString: assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
|
||||
- text: Your regex should match three criminals (<code>CCC</code>) in <code>"P1P5P4CCCP2P6P3"</code>
|
||||
testString: assert('P1P5P4CCCP2P6P3'.match(reCriminals) && 'P1P5P4CCCP2P6P3'.match(reCriminals)[0] == 'CCC');
|
||||
- text: Your regex should match five criminals (<code>CCCCC</code>) in <code>"P6P2P7P4P5CCCCCP3P1"</code>
|
||||
testString: assert('P6P2P7P4P5CCCCCP3P1'.match(reCriminals) && 'P6P2P7P4P5CCCCCP3P1'.match(reCriminals)[0] == 'CCCCC');
|
||||
- text: Your regex should not match any criminals in <code>""</code>
|
||||
testString: assert(!reCriminals.test(''));
|
||||
- text: Your regex should not match any criminals in <code>"P1P2P3"</code>
|
||||
testString: assert(!reCriminals.test('P1P2P3'));
|
||||
- text: Your regex should match fifty criminals (<code>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC</code>) in <code>"P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3"</code>.
|
||||
testString: assert('P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(reCriminals) && 'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(reCriminals)[0] == "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");
|
||||
Your regex should match one criminal (`C`) in `"C"`
|
||||
|
||||
```js
|
||||
assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should match two criminals (`CC`) in `"CC"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let reCriminals = /C+/; // Change this line
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,52 +5,83 @@ challengeType: 1
|
||||
forumTopicId: 301344
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>"A"</code>, <code>"B"</code>, and <code>"C"</code>. Examples of lowercase are <code>"a"</code>, <code>"b"</code>, and <code>"c"</code>.
|
||||
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 <code>i</code> flag. You can use it by appending it to the regex. An example of using this flag is <code>/ignorecase/i</code>. This regex can match the strings <code>"ignorecase"</code>, <code>"igNoreCase"</code>, and <code>"IgnoreCase"</code>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a regex <code>fccRegex</code> to match <code>"freeCodeCamp"</code>, no matter its case. Your regex should not match any abbreviations or variations with spaces.
|
||||
</section>
|
||||
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"`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
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"`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should match <code>freeCodeCamp</code>
|
||||
testString: assert(fccRegex.test('freeCodeCamp'));
|
||||
- text: Your regex should match <code>FreeCodeCamp</code>
|
||||
testString: assert(fccRegex.test('FreeCodeCamp'));
|
||||
- text: Your regex should match <code>FreecodeCamp</code>
|
||||
testString: assert(fccRegex.test('FreecodeCamp'));
|
||||
- text: Your regex should match <code>FreeCodecamp</code>
|
||||
testString: assert(fccRegex.test('FreeCodecamp'));
|
||||
- text: Your regex should not match <code>Free Code Camp</code>
|
||||
testString: assert(!fccRegex.test('Free Code Camp'));
|
||||
- text: Your regex should match <code>FreeCOdeCamp</code>
|
||||
testString: assert(fccRegex.test('FreeCOdeCamp'));
|
||||
- text: Your regex should not match <code>FCC</code>
|
||||
testString: assert(!fccRegex.test('FCC'));
|
||||
- text: Your regex should match <code>FrEeCoDeCamp</code>
|
||||
testString: assert(fccRegex.test('FrEeCoDeCamp'));
|
||||
- text: Your regex should match <code>FrEeCodECamp</code>
|
||||
testString: assert(fccRegex.test('FrEeCodECamp'));
|
||||
- text: Your regex should match <code>FReeCodeCAmp</code>
|
||||
testString: assert(fccRegex.test('FReeCodeCAmp'));
|
||||
# --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'));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should match `FreeCodeCamp`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(fccRegex.test('FreeCodeCamp'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -58,19 +89,10 @@ let fccRegex = /change/; // Change this line
|
||||
let result = fccRegex.test(myString);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myString = "freeCodeCamp";
|
||||
let fccRegex = /freecodecamp/i; // Change this line
|
||||
let result = fccRegex.test(myString);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,47 +5,67 @@ challengeType: 1
|
||||
forumTopicId: 301345
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Using regexes like <code>/coding/</code>, you can look for the pattern <code>"coding"</code> 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 <code>alternation</code> or <code>OR</code> operator: <code>|</code>.
|
||||
This operator matches patterns either before or after it. For example, if you wanted to match <code>"yes"</code> or <code>"no"</code>, the regex you want is <code>/yes|no/</code>.
|
||||
You can also search for more than just two patterns. You can do this by adding more patterns with more <code>OR</code> operators separating them, like <code>/yes|no|maybe/</code>.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Complete the regex <code>petRegex</code> to match the pets <code>"dog"</code>, <code>"cat"</code>, <code>"bird"</code>, or <code>"fish"</code>.
|
||||
</section>
|
||||
Using regexes like `/coding/`, you can look for the pattern `"coding"` in another string.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
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: `|`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>petRegex</code> should return <code>true</code> for the string <code>"John has a pet dog."</code>
|
||||
testString: assert(petRegex.test('John has a pet dog.'));
|
||||
- text: Your regex <code>petRegex</code> should return <code>false</code> for the string <code>"Emma has a pet rock."</code>
|
||||
testString: assert(!petRegex.test('Emma has a pet rock.'));
|
||||
- text: Your regex <code>petRegex</code> should return <code>true</code> for the string <code>"Emma has a pet bird."</code>
|
||||
testString: assert(petRegex.test('Emma has a pet bird.'));
|
||||
- text: Your regex <code>petRegex</code> should return <code>true</code> for the string <code>"Liz has a pet cat."</code>
|
||||
testString: assert(petRegex.test('Liz has a pet cat.'));
|
||||
- text: Your regex <code>petRegex</code> should return <code>false</code> for the string <code>"Kara has a pet dolphin."</code>
|
||||
testString: assert(!petRegex.test('Kara has a pet dolphin.'));
|
||||
- text: Your regex <code>petRegex</code> should return <code>true</code> for the string <code>"Alice has a pet fish."</code>
|
||||
testString: assert(petRegex.test('Alice has a pet fish.'));
|
||||
- text: Your regex <code>petRegex</code> should return <code>false</code> for the string <code>"Jimmy has a pet computer."</code>
|
||||
testString: assert(!petRegex.test('Jimmy has a pet computer.'));
|
||||
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.'));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `petRegex` should return `false` for the string `"Emma has a pet rock."`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!petRegex.test('Emma has a pet rock.'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -53,19 +73,10 @@ let petRegex = /change/; // Change this line
|
||||
let result = petRegex.test(petString);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let petString = "James has a pet cat.";
|
||||
let petRegex = /dog|cat|bird|fish/; // Change this line
|
||||
let result = petRegex.test(petString);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301346
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Using character classes, you were able to search for all letters of the alphabet with <code>[a-z]</code>. 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 <code>\w</code>. This shortcut is equal to <code>[A-Za-z0-9_]</code>. This character class matches upper and lowercase letters plus numbers. Note, this character class also includes the underscore character (<code>_</code>).
|
||||
# --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_]+/;
|
||||
@ -22,39 +23,62 @@ shortHand.test(varNames); // Returns true
|
||||
```
|
||||
|
||||
These shortcut character classes are also known as <dfn>shorthand character classes</dfn>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the shorthand character class <code>\w</code> to count the number of alphanumeric characters in various quotes and strings.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Use the shorthand character class `\w` to count the number of alphanumeric characters in various quotes and strings.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the global flag.
|
||||
testString: assert(alphabetRegexV2.global);
|
||||
- text: Your regex should use the shorthand character <code>\w</code> to match all characters which are alphanumeric.
|
||||
testString: assert(/\\w/.test(alphabetRegexV2.source));
|
||||
- text: Your regex should find 31 alphanumeric characters in <code>"The five boxing wizards jump quickly."</code>
|
||||
testString: assert("The five boxing wizards jump quickly.".match(alphabetRegexV2).length === 31);
|
||||
- text: Your regex should find 32 alphanumeric characters in <code>"Pack my box with five dozen liquor jugs."</code>
|
||||
testString: assert("Pack my box with five dozen liquor jugs.".match(alphabetRegexV2).length === 32);
|
||||
- text: Your regex should find 30 alphanumeric characters in <code>"How vexingly quick daft zebras jump!"</code>
|
||||
testString: assert("How vexingly quick daft zebras jump!".match(alphabetRegexV2).length === 30);
|
||||
- text: Your regex should find 36 alphanumeric characters in <code>"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."</code>
|
||||
testString: assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(alphabetRegexV2).length === 36);
|
||||
# --hints--
|
||||
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(alphabetRegexV2.global);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should use the shorthand character `\w` to match all characters which are alphanumeric.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/\\w/.test(alphabetRegexV2.source));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -62,19 +86,10 @@ let alphabetRegexV2 = /change/; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let alphabetRegexV2 = /\w/g; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,47 +5,69 @@ challengeType: 1
|
||||
forumTopicId: 301347
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The last challenge showed how to search for digits using the shortcut <code>\d</code> with a lowercase <code>d</code>. You can also search for non-digits using a similar shortcut that uses an uppercase <code>D</code> instead.
|
||||
The shortcut to look for non-digit characters is <code>\D</code>. This is equal to the character class <code>[^0-9]</code>, which looks for a single character that is not a number between zero and nine.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the shorthand character class for non-digits <code>\D</code> to count how many non-digits are in movie titles.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
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.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the shortcut character to match non-digit characters
|
||||
testString: assert(/\\D/.test(noNumRegex.source));
|
||||
- text: Your regex should use the global flag.
|
||||
testString: assert(noNumRegex.global);
|
||||
- text: Your regex should find no non-digits in <code>"9"</code>.
|
||||
testString: assert("9".match(noNumRegex) == null);
|
||||
- text: Your regex should find 6 non-digits in <code>"Catch 22"</code>.
|
||||
testString: assert("Catch 22".match(noNumRegex).length == 6);
|
||||
- text: Your regex should find 11 non-digits in <code>"101 Dalmatians"</code>.
|
||||
testString: assert("101 Dalmatians".match(noNumRegex).length == 11);
|
||||
- text: Your regex should find 15 non-digits in <code>"One, Two, Three"</code>.
|
||||
testString: assert("One, Two, Three".match(noNumRegex).length == 15);
|
||||
- text: Your regex should find 12 non-digits in <code>"21 Jump Street"</code>.
|
||||
testString: assert("21 Jump Street".match(noNumRegex).length == 12);
|
||||
- text: 'Your regex should find 17 non-digits in <code>"2001: A Space Odyssey"</code>.'
|
||||
testString: 'assert("2001: A Space Odyssey".match(noNumRegex).length == 17);'
|
||||
# --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));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should use the global flag.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(noNumRegex.global);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -53,19 +75,10 @@ let noNumRegex = /change/; // Change this line
|
||||
let result = movieName.match(noNumRegex).length;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let noNumRegex = /\D/g; // Change this line
|
||||
let result = movieName.match(noNumRegex).length;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,47 +5,69 @@ challengeType: 1
|
||||
forumTopicId: 18181
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>\d</code>, with a lowercase <code>d</code>. This is equal to the character class <code>[0-9]</code>, which looks for a single character of any number between zero and nine.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the shorthand character class <code>\d</code> to count how many digits are in movie titles. Written out numbers ("six" instead of 6) do not count.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the shortcut character to match digit characters
|
||||
testString: assert(/\\d/.test(numRegex.source));
|
||||
- text: Your regex should use the global flag.
|
||||
testString: assert(numRegex.global);
|
||||
- text: Your regex should find 1 digit in <code>"9"</code>.
|
||||
testString: assert("9".match(numRegex).length == 1);
|
||||
- text: Your regex should find 2 digits in <code>"Catch 22"</code>.
|
||||
testString: assert("Catch 22".match(numRegex).length == 2);
|
||||
- text: Your regex should find 3 digits in <code>"101 Dalmatians"</code>.
|
||||
testString: assert("101 Dalmatians".match(numRegex).length == 3);
|
||||
- text: Your regex should find no digits in <code>"One, Two, Three"</code>.
|
||||
testString: assert("One, Two, Three".match(numRegex) == null);
|
||||
- text: Your regex should find 2 digits in <code>"21 Jump Street"</code>.
|
||||
testString: assert("21 Jump Street".match(numRegex).length == 2);
|
||||
- text: 'Your regex should find 4 digits in <code>"2001: A Space Odyssey"</code>.'
|
||||
testString: 'assert("2001: A Space Odyssey".match(numRegex).length == 4);'
|
||||
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));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should use the global flag.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(numRegex.global);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -53,20 +75,10 @@ let numRegex = /change/; // Change this line
|
||||
let result = movieName.match(numRegex).length;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let numRegex = /\d/g; // Change this line
|
||||
let result = movieName.match(numRegex).length;
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301348
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='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: <code>.</code>
|
||||
The wildcard character <code>.</code> will match any one character. The wildcard is also called <code>dot</code> and <code>period</code>. You can use the wildcard character just like any other character in the regex. For example, if you wanted to match <code>"hug"</code>, <code>"huh"</code>, <code>"hut"</code>, and <code>"hum"</code>, you can use the regex <code>/hu./</code> to match all four words.
|
||||
# --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";
|
||||
@ -18,47 +19,87 @@ huRegex.test(humStr); // Returns true
|
||||
huRegex.test(hugStr); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Complete the regex <code>unRegex</code> so that it matches the strings <code>"run"</code>, <code>"sun"</code>, <code>"fun"</code>, <code>"pun"</code>, <code>"nun"</code>, and <code>"bun"</code>. Your regex should use the wildcard character.
|
||||
</section>
|
||||
Complete the regex `unRegex` so that it matches the strings `"run"`, `"sun"`, `"fun"`, `"pun"`, `"nun"`, and `"bun"`. Your regex should use the wildcard character.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should use the <code>.test()</code> method.
|
||||
testString: assert(code.match(/\.test\(.*\)/));
|
||||
- text: You should use the wildcard character in your regex <code>unRegex</code>
|
||||
testString: assert(/\./.test(unRegex.source));
|
||||
- text: Your regex <code>unRegex</code> should match <code>"run"</code> in <code>"Let us go on a run."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(unRegex.test("Let us go on a run."));
|
||||
- text: Your regex <code>unRegex</code> should match <code>"sun"</code> in <code>"The sun is out today."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(unRegex.test("The sun is out today."));
|
||||
- text: Your regex <code>unRegex</code> should match <code>"fun"</code> in <code>"Coding is a lot of fun."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(unRegex.test("Coding is a lot of fun."));
|
||||
- text: Your regex <code>unRegex</code> should match <code>"pun"</code> in <code>"Seven days without a pun makes one weak."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(unRegex.test("Seven days without a pun makes one weak."));
|
||||
- text: Your regex <code>unRegex</code> should match <code>"nun"</code> in <code>"One takes a vow to be a nun."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(unRegex.test("One takes a vow to be a nun."));
|
||||
- text: Your regex <code>unRegex</code> should match <code>"bun"</code> in <code>"She got fired from the hot dog stand for putting her hair in a bun."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(unRegex.test("She got fired from the hot dog stand for putting her hair in a bun."));
|
||||
- text: Your regex <code>unRegex</code> should not match <code>"There is a bug in my code."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(!unRegex.test("There is a bug in my code."));
|
||||
- text: Your regex <code>unRegex</code> should not match <code>"Catch me if you can."</code>
|
||||
testString: unRegex.lastIndex = 0; assert(!unRegex.test("Catch me if you can."));
|
||||
You should use the `.test()` method.
|
||||
|
||||
```js
|
||||
assert(code.match(/\.test\(.*\)/));
|
||||
```
|
||||
|
||||
</section>
|
||||
You should use the wildcard character in your regex `unRegex`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/\./.test(unRegex.source));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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!";
|
||||
@ -66,19 +107,10 @@ let unRegex = /change/; // Change this line
|
||||
let result = unRegex.test(exampleStr);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let exampleStr = "Let's have fun with regular expressions!";
|
||||
let unRegex = /.un/; // Change this line
|
||||
let result = unRegex.test(exampleStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301349
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 (<code>^</code>) inside a character set to create a negated character set in the form <code>[^thingsThatWillNotBeMatched]</code>. Outside of a character set, the caret is used to search for patterns at the beginning of 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.";
|
||||
@ -20,35 +21,39 @@ firstRegex.test(notFirst);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the caret character in a regex to find <code>"Cal"</code> only in the beginning of the string <code>rickyAndCal</code>.
|
||||
</section>
|
||||
Use the caret character in a regex to find `"Cal"` only in the beginning of the string `rickyAndCal`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should search for <code>"Cal"</code> with a capital letter.
|
||||
testString: assert(calRegex.source == "^Cal");
|
||||
- text: Your regex should not use any flags.
|
||||
testString: assert(calRegex.flags == "");
|
||||
- text: Your regex should match <code>"Cal"</code> at the beginning of the string.
|
||||
testString: assert(calRegex.test("Cal and Ricky both like racing."));
|
||||
- text: Your regex should not match <code>"Cal"</code> in the middle of a string.
|
||||
testString: assert(!calRegex.test("Ricky and Cal both like racing."));
|
||||
Your regex should search for `"Cal"` with a capital letter.
|
||||
|
||||
```js
|
||||
assert(calRegex.source == '^Cal');
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should not use any flags.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(calRegex.flags == '');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -56,19 +61,10 @@ let calRegex = /change/; // Change this line
|
||||
let result = calRegex.test(rickyAndCal);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let rickyAndCal = "Cal and Ricky both like racing.";
|
||||
let calRegex = /^Cal/; // Change this line
|
||||
let result = calRegex.test(rickyAndCal);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,39 +5,43 @@ challengeType: 1
|
||||
forumTopicId: 301350
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>+</code> 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, <code>/a+/g</code> would find one match in <code>"abc"</code> and return <code>["a"]</code>. Because of the <code>+</code>, it would also find a single match in <code>"aabc"</code> and return <code>["aa"]</code>.
|
||||
If it were instead checking the string <code>"abab"</code>, it would find two matches and return <code>["a", "a"]</code> because the <code>a</code> characters are not in a row - there is a <code>b</code> between them. Finally, since there is no <code>"a"</code> in the string <code>"bcd"</code>, it wouldn't find a match.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
You want to find matches when the letter <code>s</code> occurs one or more times in <code>"Mississippi"</code>. Write a regex that uses the <code>+</code> sign.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
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"]`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>myRegex</code> should use the <code>+</code> sign to match one or more <code>s</code> characters.
|
||||
testString: assert(/\+/.test(myRegex.source));
|
||||
- text: Your regex <code>myRegex</code> should match 2 items.
|
||||
testString: assert(result.length == 2);
|
||||
- text: The <code>result</code> variable should be an array with two matches of <code>"ss"</code>
|
||||
testString: assert(result[0] == 'ss' && result[1] == 'ss');
|
||||
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));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `myRegex` should match 2 items.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(result.length == 2);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -45,19 +49,10 @@ let myRegex = /change/; // Change this line
|
||||
let result = difficultSpelling.match(myRegex);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let difficultSpelling = "Mississippi";
|
||||
let myRegex = /s+/g; // Change this line
|
||||
let result = difficultSpelling.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301351
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The last challenge used the plus <code>+</code> 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: <code>*</code>.
|
||||
# --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!";
|
||||
@ -20,39 +21,61 @@ gPhrase.match(goRegex); // Returns ["g"]
|
||||
oPhrase.match(goRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
For this challenge, <code>chewieQuote</code> has been initialized as "Aaaaaaaaaaaaaaaarrrgh!" behind the scenes. Create a regex <code>chewieRegex</code> that uses the <code>*</code> character to match an uppercase <code>"A"</code> character immediately followed by zero or more lowercase <code>"a"</code> characters in <code>chewieQuote</code>. Your regex does not need flags or character classes, and it should not match any of the other quotes.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>chewieRegex</code> should use the <code>*</code> character to match zero or more <code>a</code> characters.
|
||||
testString: assert(/\*/.test(chewieRegex.source));
|
||||
- text: Your regex should match <code>"A"</code> in <code>chewieQuote</code>.
|
||||
testString: assert(result[0][0] === 'A');
|
||||
- text: Your regex should match <code>"Aaaaaaaaaaaaaaaa"</code> in <code>chewieQuote</code>.
|
||||
testString: assert(result[0] === 'Aaaaaaaaaaaaaaaa');
|
||||
- text: Your regex <code>chewieRegex</code> should match 16 characters in <code>chewieQuote</code>.
|
||||
testString: assert(result[0].length === 16);
|
||||
- text: Your regex should not match any characters in "He made a fair move. Screaming about it can't help you."
|
||||
testString: assert(!"He made a fair move. Screaming about it can't help you.".match(chewieRegex));
|
||||
- text: Your regex should not match any characters in "Let him have it. It's not wise to upset a Wookiee."
|
||||
testString: assert(!"Let him have it. It's not wise to upset a Wookiee.".match(chewieRegex));
|
||||
Your regex `chewieRegex` should use the `*` character to match zero or more `a` characters.
|
||||
|
||||
```js
|
||||
assert(/\*/.test(chewieRegex.source));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should match `"A"` in `chewieQuote`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(result[0][0] === 'A');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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
|
||||
@ -62,25 +85,9 @@ let chewieRegex = /change/; // Change this line
|
||||
let result = chewieQuote.match(chewieRegex);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let chewieRegex = /Aa*/;
|
||||
let result = chewieQuote.match(chewieRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301352
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>$</code> at the end of the regex.
|
||||
|
||||
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";
|
||||
@ -21,33 +22,33 @@ storyRegex.test(noEnding);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the anchor character (<code>$</code>) to match the string <code>"caboose"</code> at the end of the string <code>caboose</code>.
|
||||
</section>
|
||||
Use the anchor character (`$`) to match the string `"caboose"` at the end of the string `caboose`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should search for <code>"caboose"</code> with the dollar sign <code>$</code> anchor in your regex.
|
||||
testString: assert(lastRegex.source == "caboose$");
|
||||
- text: Your regex should not use any flags.
|
||||
testString: assert(lastRegex.flags == "");
|
||||
- text: You should match <code>"caboose"</code> at the end of the string <code>"The last car on a train is the caboose"</code>
|
||||
testString: assert(lastRegex.test("The last car on a train is the caboose"));
|
||||
You should search for `"caboose"` with the dollar sign `$` anchor in your regex.
|
||||
|
||||
```js
|
||||
assert(lastRegex.source == 'caboose$');
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should not use any flags.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(lastRegex.flags == '');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -55,19 +56,10 @@ let lastRegex = /change/; // Change this line
|
||||
let result = lastRegex.test(caboose);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let caboose = "The last car on a train is the caboose";
|
||||
let lastRegex = /caboose$/; // Change this line
|
||||
let result = lastRegex.test(caboose);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301353
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You've learned that you can use a shortcut to match alphanumerics <code>[A-Za-z0-9_]</code> using <code>\w</code>. A natural pattern you might want to search for is the opposite of alphanumerics.
|
||||
You can search for the opposite of the <code>\w</code> with <code>\W</code>. Note, the opposite pattern uses a capital letter. This shortcut is the same as <code>[^A-Za-z0-9_]</code>.
|
||||
# --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/;
|
||||
@ -18,39 +19,60 @@ numbers.match(shortHand); // Returns ["%"]
|
||||
sentence.match(shortHand); // Returns ["!"]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the shorthand character class <code>\W</code> to count the number of non-alphanumeric characters in various quotes and strings.
|
||||
</section>
|
||||
Use the shorthand character class `\W` to count the number of non-alphanumeric characters in various quotes and strings.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the global flag.
|
||||
testString: assert(nonAlphabetRegex.global);
|
||||
- text: Your regex should find 6 non-alphanumeric characters in <code>"The five boxing wizards jump quickly."</code>.
|
||||
testString: assert("The five boxing wizards jump quickly.".match(nonAlphabetRegex).length == 6);
|
||||
- text: Your regex should use the shorthand character to match characters which are non-alphanumeric.
|
||||
testString: assert(/\\W/.test(nonAlphabetRegex.source));
|
||||
- text: Your regex should find 8 non-alphanumeric characters in <code>"Pack my box with five dozen liquor jugs."</code>
|
||||
testString: assert("Pack my box with five dozen liquor jugs.".match(nonAlphabetRegex).length == 8);
|
||||
- text: Your regex should find 6 non-alphanumeric characters in <code>"How vexingly quick daft zebras jump!"</code>
|
||||
testString: assert("How vexingly quick daft zebras jump!".match(nonAlphabetRegex).length == 6);
|
||||
- text: Your regex should find 12 non-alphanumeric characters in <code>"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."</code>
|
||||
testString: assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(nonAlphabetRegex).length == 12);
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(nonAlphabetRegex.global);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should find 6 non-alphanumeric characters in `"The five boxing wizards jump quickly."`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
'The five boxing wizards jump quickly.'.match(nonAlphabetRegex).length == 6
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -58,19 +80,10 @@ let nonAlphabetRegex = /change/; // Change this line
|
||||
let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let quoteSample = "The five boxing wizards_jump quickly.";
|
||||
let nonAlphabetRegex = /\W/g; // Change this line
|
||||
let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301354
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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: <code>-</code>.
|
||||
For example, to match lowercase letters <code>a</code> through <code>e</code> you would use <code>[a-e]</code>.
|
||||
|
||||
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";
|
||||
@ -21,34 +23,36 @@ batStr.match(bgRegex); // Returns ["bat"]
|
||||
matStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Match all the letters in the string <code>quoteSample</code>.
|
||||
<strong>Note</strong><br>Be sure to match both upper- and lowercase <strong>letters<strong>.
|
||||
</section>
|
||||
Match all the letters in the string `quoteSample`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note**
|
||||
Be sure to match both upper- and lowercase **letters**.\*\*\*\*
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>alphabetRegex</code> should match 35 items.
|
||||
testString: assert(result.length == 35);
|
||||
- text: Your regex <code>alphabetRegex</code> should use the global flag.
|
||||
testString: assert(alphabetRegex.flags.match(/g/).length == 1);
|
||||
- text: Your regex <code>alphabetRegex</code> should use the case insensitive flag.
|
||||
testString: assert(alphabetRegex.flags.match(/i/).length == 1);
|
||||
# --hints--
|
||||
|
||||
Your regex `alphabetRegex` should match 35 items.
|
||||
|
||||
```js
|
||||
assert(result.length == 35);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `alphabetRegex` should use the global flag.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(alphabetRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -56,19 +60,10 @@ let alphabetRegex = /change/; // Change this line
|
||||
let result = alphabetRegex; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --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
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,9 +5,9 @@ challengeType: 1
|
||||
forumTopicId: 301355
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In the last challenge, you searched for the word <code>"Hello"</code> using the regular expression <code>/Hello/</code>. That regex searched for a literal match of the string <code>"Hello"</code>. Here's another example searching for a literal match of the string <code>"Kevin"</code>:
|
||||
# --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.";
|
||||
@ -16,7 +16,7 @@ testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
Any other forms of <code>"Kevin"</code> will not match. For example, the regex <code>/Kevin/</code> will not match <code>"kevin"</code> or <code>"KEVIN"</code>.
|
||||
Any other forms of `"Kevin"` will not match. For example, the regex `/Kevin/` will not match `"kevin"` or `"KEVIN"`.
|
||||
|
||||
```js
|
||||
let wrongRegex = /kevin/;
|
||||
@ -25,33 +25,34 @@ wrongRegex.test(testStr);
|
||||
```
|
||||
|
||||
A future challenge will show how to match those other forms as well.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Complete the regex <code>waldoRegex</code> to find <code>"Waldo"</code> in the string <code>waldoIsHiding</code> with a literal match.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Complete the regex `waldoRegex` to find `"Waldo"` in the string `waldoIsHiding` with a literal match.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>waldoRegex</code> should find <code>"Waldo"</code>
|
||||
testString: assert(waldoRegex.test(waldoIsHiding));
|
||||
- text: Your regex <code>waldoRegex</code> should not search for anything else.
|
||||
testString: assert(!waldoRegex.test('Somewhere is hiding in this text.'));
|
||||
- text: You should perform a literal string match with your regex.
|
||||
testString: assert(!/\/.*\/i/.test(code));
|
||||
# --hints--
|
||||
|
||||
Your regex `waldoRegex` should find `"Waldo"`
|
||||
|
||||
```js
|
||||
assert(waldoRegex.test(waldoIsHiding));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `waldoRegex` should not search for anything else.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!waldoRegex.test('Somewhere is hiding in this text.'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -59,19 +60,10 @@ let waldoRegex = /search/; // Change this line
|
||||
let result = waldoRegex.test(waldoIsHiding);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
||||
let waldoRegex = /Waldo/; // Change this line
|
||||
let result = waldoRegex.test(waldoIsHiding);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 18210
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You learned about searching for whitespace using <code>\s</code>, with a lowercase <code>s</code>. You can also search for everything except whitespace.
|
||||
Search for non-whitespace using <code>\S</code>, which is an uppercase <code>s</code>. 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 <code>[^ \r\t\f\n\v]</code>.
|
||||
# --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!"
|
||||
@ -16,37 +17,48 @@ let nonSpaceRegex = /\S/g;
|
||||
whiteSpace.match(nonSpaceRegex).length; // Returns 32
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the regex <code>countNonWhiteSpace</code> to look for multiple non-whitespace characters in a string.
|
||||
</section>
|
||||
Change the regex `countNonWhiteSpace` to look for multiple non-whitespace characters in a string.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the global flag.
|
||||
testString: assert(countNonWhiteSpace.global);
|
||||
- text: Your regex should use the shorthand character <code>\S</code> to match all non-whitespace characters.
|
||||
testString: assert(/\\S/.test(countNonWhiteSpace.source));
|
||||
- text: Your regex should find 35 non-spaces in <code>"Men are from Mars and women are from Venus."</code>
|
||||
testString: assert("Men are from Mars and women are from Venus.".match(countNonWhiteSpace).length == 35);
|
||||
- text: 'Your regex should find 23 non-spaces in <code>"Space: the final frontier."</code>'
|
||||
testString: 'assert("Space: the final frontier.".match(countNonWhiteSpace).length == 23);'
|
||||
- text: Your regex should find 21 non-spaces in <code>"MindYourPersonalSpace"</code>
|
||||
testString: assert("MindYourPersonalSpace".match(countNonWhiteSpace).length == 21);
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(countNonWhiteSpace.global);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should use the shorthand character `\S` to match all non-whitespace characters.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/\\S/.test(countNonWhiteSpace.source));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -54,19 +66,10 @@ let countNonWhiteSpace = /change/; // Change this line
|
||||
let result = sample.match(countNonWhiteSpace);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countNonWhiteSpace = /\S/g; // Change this line
|
||||
let result = sample.match(countNonWhiteSpace);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,12 @@ challengeType: 1
|
||||
forumTopicId: 301356
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Using the hyphen (<code>-</code>) to match a range of characters is not limited to letters. It also works to match a range of numbers.
|
||||
For example, <code>/[0-5]/</code> matches any number between <code>0</code> and <code>5</code>, including the <code>0</code> and <code>5</code>.
|
||||
# --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
|
||||
@ -18,33 +20,33 @@ let myRegex = /[a-z0-9]/ig;
|
||||
jennyStr.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a single regex that matches a range of letters between <code>h</code> and <code>s</code>, and a range of numbers between <code>2</code> and <code>6</code>. Remember to include the appropriate flags in the regex.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>myRegex</code> should match 17 items.
|
||||
testString: assert(result.length == 17);
|
||||
- text: Your regex <code>myRegex</code> should use the global flag.
|
||||
testString: assert(myRegex.flags.match(/g/).length == 1);
|
||||
- text: Your regex <code>myRegex</code> should use the case insensitive flag.
|
||||
testString: assert(myRegex.flags.match(/i/).length == 1);
|
||||
Your regex `myRegex` should match 17 items.
|
||||
|
||||
```js
|
||||
assert(result.length == 17);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `myRegex` should use the global flag.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(myRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -52,20 +54,10 @@ let myRegex = /change/; // Change this line
|
||||
let result = myRegex; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --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
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301357
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You learned how to match literal patterns (<code>/literal/</code>) and wildcard character (<code>/./</code>). 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 (<code>[</code> and <code>]</code>) brackets.
|
||||
For example, you want to match <code>"bag"</code>, <code>"big"</code>, and <code>"bug"</code> but not <code>"bog"</code>. You can create the regex <code>/b[aiu]g/</code> to do this. The <code>[aiu]</code> is the character class that will only match the characters <code>"a"</code>, <code>"i"</code>, or <code>"u"</code>.
|
||||
# --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";
|
||||
@ -23,38 +25,48 @@ bugStr.match(bgRegex); // Returns ["bug"]
|
||||
bogStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use a character class with vowels (<code>a</code>, <code>e</code>, <code>i</code>, <code>o</code>, <code>u</code>) in your regex <code>vowelRegex</code> to find all the vowels in the string <code>quoteSample</code>.
|
||||
<strong>Note</strong><br>Be sure to match both upper- and lowercase vowels.
|
||||
</section>
|
||||
Use a character class with vowels (`a`, `e`, `i`, `o`, `u`) in your regex `vowelRegex` to find all the vowels in the string `quoteSample`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note**
|
||||
Be sure to match both upper- and lowercase vowels.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should find all 25 vowels.
|
||||
testString: assert(result.length == 25);
|
||||
- text: Your regex <code>vowelRegex</code> should use a character class.
|
||||
testString: assert(/\[.*\]/.test(vowelRegex.source));
|
||||
- text: Your regex <code>vowelRegex</code> should use the global flag.
|
||||
testString: assert(vowelRegex.flags.match(/g/).length == 1);
|
||||
- text: Your regex <code>vowelRegex</code> should use the case insensitive flag.
|
||||
testString: assert(vowelRegex.flags.match(/i/).length == 1);
|
||||
- text: Your regex should not match any consonants.
|
||||
testString: assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));
|
||||
# --hints--
|
||||
|
||||
You should find all 25 vowels.
|
||||
|
||||
```js
|
||||
assert(result.length == 25);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `vowelRegex` should use a character class.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/\[.*\]/.test(vowelRegex.source));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -62,19 +74,10 @@ let vowelRegex = /change/; // Change this line
|
||||
let result = vowelRegex; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --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
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,38 +5,41 @@ challengeType: 1
|
||||
forumTopicId: 301358
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 (<code>^</code>) after the opening bracket and before the characters you do not want to match.
|
||||
For example, <code>/[^aeiou]/gi</code> matches all characters that are not a vowel. Note that characters like <code>.</code>, <code>!</code>, <code>[</code>, <code>@</code>, <code>/</code> and white space are matched - the negated vowel character set only excludes the vowel characters.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
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.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex <code>myRegex</code> should match 9 items.
|
||||
testString: assert(result.length == 9);
|
||||
- text: Your regex <code>myRegex</code> should use the global flag.
|
||||
testString: assert(myRegex.flags.match(/g/).length == 1);
|
||||
- text: Your regex <code>myRegex</code> should use the case insensitive flag.
|
||||
testString: assert(myRegex.flags.match(/i/).length == 1);
|
||||
Your regex `myRegex` should match 9 items.
|
||||
|
||||
```js
|
||||
assert(result.length == 9);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex `myRegex` should use the global flag.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(myRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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.";
|
||||
@ -44,19 +47,10 @@ let myRegex = /change/; // Change this line
|
||||
let result = myRegex; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let quoteSample = "3 blind mice.";
|
||||
let myRegex = /[^0-9aeiou]/gi; // Change this line
|
||||
let result = quoteSample.match(myRegex); // Change this line
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301359
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>\s</code>, which is a lowercase <code>s</code>. 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 <code>[ \r\t\f\n\v]</code>.
|
||||
|
||||
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!"
|
||||
@ -17,37 +18,48 @@ whiteSpace.match(spaceRegex);
|
||||
// Returns [" ", " "]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the regex <code>countWhiteSpace</code> to look for multiple whitespace characters in a string.
|
||||
</section>
|
||||
Change the regex `countWhiteSpace` to look for multiple whitespace characters in a string.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the global flag.
|
||||
testString: assert(countWhiteSpace.global);
|
||||
- text: Your regex should use the shorthand character <code>\s</code> to match all whitespace characters.
|
||||
testString: assert(/\\s/.test(countWhiteSpace.source));
|
||||
- text: Your regex should find eight spaces in <code>"Men are from Mars and women are from Venus."</code>
|
||||
testString: assert("Men are from Mars and women are from Venus.".match(countWhiteSpace).length == 8);
|
||||
- text: 'Your regex should find three spaces in <code>"Space: the final frontier."</code>'
|
||||
testString: 'assert("Space: the final frontier.".match(countWhiteSpace).length == 3);'
|
||||
- text: Your regex should find no spaces in <code>"MindYourPersonalSpace"</code>
|
||||
testString: assert("MindYourPersonalSpace".match(countWhiteSpace) == null);
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(countWhiteSpace.global);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should use the shorthand character `\s` to match all whitespace characters.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/\\s/.test(countWhiteSpace.source));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -55,19 +67,10 @@ let countWhiteSpace = /change/; // Change this line
|
||||
let result = sample.match(countWhiteSpace);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countWhiteSpace = /\s/g;
|
||||
let result = sample.match(countWhiteSpace);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,12 +5,16 @@ challengeType: 1
|
||||
forumTopicId: 301360
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>(?=...)</code> where the <code>...</code> 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 <code>(?!...)</code> where the <code>...</code> 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.
|
||||
|
||||
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
|
||||
@ -30,45 +34,69 @@ let checkPass = /(?=\w{3,6})(?=\D*\d)/;
|
||||
checkPass.test(password); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use lookaheads in the <code>pwRegex</code> to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.
|
||||
</section>
|
||||
Use lookaheads in the `pwRegex` to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use two positive <code>lookaheads</code>.
|
||||
testString: assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
||||
- text: Your regex should not match <code>"astronaut"</code>
|
||||
testString: assert(!pwRegex.test("astronaut"));
|
||||
- text: Your regex should not match <code>"banan1"</code>
|
||||
testString: assert(!pwRegex.test("banan1"));
|
||||
- text: Your regex should match <code>"bana12"</code>
|
||||
testString: assert(pwRegex.test("bana12"));
|
||||
- text: Your regex should match <code>"abc123"</code>
|
||||
testString: assert(pwRegex.test("abc123"));
|
||||
- text: Your regex should not match <code>"1234"</code>
|
||||
testString: assert(!pwRegex.test("1234"));
|
||||
- text: Your regex should not match <code>"8pass99"</code>
|
||||
testString: assert(!pwRegex.test("8pass99"));
|
||||
- text: Your regex should not match <code>"12abcde"</code>
|
||||
testString: assert(!pwRegex.test("12abcde"));
|
||||
- text: Your regex should match <code>"astr1on11aut"</code>
|
||||
testString: assert(pwRegex.test("astr1on11aut"));
|
||||
Your regex should use two positive `lookaheads`.
|
||||
|
||||
```js
|
||||
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should not match `"astronaut"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!pwRegex.test('astronaut'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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 `"1234"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('1234'));
|
||||
```
|
||||
|
||||
Your regex should not match `"8pass99"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('8pass99'));
|
||||
```
|
||||
|
||||
Your regex should not match `"12abcde"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('12abcde'));
|
||||
```
|
||||
|
||||
Your regex should match `"astr1on11aut"`
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('astr1on11aut'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let sampleWord = "astronaut";
|
||||
@ -76,18 +104,8 @@ let pwRegex = /change/; // Change this line
|
||||
let result = pwRegex.test(sampleWord);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var pwRegex = /^\D(?=\w{5})(?=\w*\d{2})/;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,37 +5,39 @@ challengeType: 1
|
||||
forumTopicId: 301362
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
# --instructions--
|
||||
|
||||
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
|
||||
<strong>Note:</strong> The <code>String.prototype.trim()</code> method would work here, but you'll need to complete this challenge using regular expressions.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note:** The `String.prototype.trim()` method would work here, but you'll need to complete this challenge using regular expressions.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>result</code> should equal to <code>"Hello, World!"</code>
|
||||
testString: assert(result == "Hello, World!");
|
||||
- text: Your solution should not use the <code>String.prototype.trim()</code> method.
|
||||
testString: assert(!code.match(/\.?[\s\S]*?trim/));
|
||||
- text: The <code>result</code> variable should not be set equal to a string.
|
||||
testString: assert(!code.match(/result\s*=\s*".*?"/));
|
||||
# --hints--
|
||||
|
||||
`result` should equal to `"Hello, World!"`
|
||||
|
||||
```js
|
||||
assert(result == 'Hello, World!');
|
||||
```
|
||||
|
||||
</section>
|
||||
Your solution should not use the `String.prototype.trim()` method.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!code.match(/\.?[\s\S]*?trim/));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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! ";
|
||||
@ -43,19 +45,10 @@ let wsRegex = /change/; // Change this line
|
||||
let result = hello; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let hello = " Hello, World! ";
|
||||
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
|
||||
let result = hello.replace(wsRegex, '$2');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,59 +5,101 @@ challengeType: 1
|
||||
forumTopicId: 301363
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the regex <code>userCheck</code> to fit the constraints listed above.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Change the regex `userCheck` to fit the constraints listed above.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should match <code>JACK</code>
|
||||
testString: assert(userCheck.test("JACK"));
|
||||
- text: Your regex should not match <code>J</code>
|
||||
testString: assert(!userCheck.test("J"));
|
||||
- text: Your regex should match <code>Jo</code>
|
||||
testString: assert(userCheck.test("Jo"));
|
||||
- text: Your regex should match <code>Oceans11</code>
|
||||
testString: assert(userCheck.test("Oceans11"));
|
||||
- text: Your regex should match <code>RegexGuru</code>
|
||||
testString: assert(userCheck.test("RegexGuru"));
|
||||
- text: Your regex should not match <code>007</code>
|
||||
testString: assert(!userCheck.test("007"));
|
||||
- text: Your regex should not match <code>9</code>
|
||||
testString: assert(!userCheck.test("9"));
|
||||
- text: Your regex should not match <code>A1</code>
|
||||
testString: assert(!userCheck.test("A1"));
|
||||
- text: Your regex should not match <code>BadUs3rnam3</code>
|
||||
testString: assert(!userCheck.test("BadUs3rnam3"));
|
||||
- text: Your regex should match <code>Z97</code>
|
||||
testString: assert(userCheck.test("Z97"));
|
||||
- text: Your regex should not match <code>c57bT3</code>
|
||||
testString: assert(!userCheck.test("c57bT3"));
|
||||
- text: Your regex should match <code>AB1</code>
|
||||
testString: assert(userCheck.test("AB1"));
|
||||
# --hints--
|
||||
|
||||
Your regex should match `JACK`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('JACK'));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should not match `J`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!userCheck.test('J'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -65,19 +107,10 @@ let userCheck = /change/; // Change this line
|
||||
let result = userCheck.test(username);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let username = "JackOfAllTrades";
|
||||
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
|
||||
let result = userCheck.test(username);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,14 @@ challengeType: 1
|
||||
forumTopicId: 301364
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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, <code>(</code> and <code>)</code>, 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 <code>\1</code> to match the first group.
|
||||
|
||||
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 (`\`) 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
|
||||
@ -19,48 +22,80 @@ repeatRegex.test(repeatStr); // Returns true
|
||||
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
|
||||
```
|
||||
|
||||
Using the <code>.match()</code> method on a string will return an array with the string it matches, along with its capture group.
|
||||
</section>
|
||||
Using the `.match()` method on a string will return an array with the string it matches, along with its capture group.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use capture groups in <code>reRegex</code> to match numbers that are repeated only three times in a string, each separated by a space.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Use capture groups in `reRegex` to match numbers that are repeated only three times in a string, each separated by a space.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use the shorthand character class for digits.
|
||||
testString: assert(reRegex.source.match(/\\d/));
|
||||
- text: Your regex should reuse a capture group twice.
|
||||
testString: assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
||||
- text: Your regex should have two spaces separating the three numbers.
|
||||
testString: assert(reRegex.source.match(/ |\\s/g).length === 2 || reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g));
|
||||
- text: Your regex should match <code>"42 42 42"</code>.
|
||||
testString: assert(reRegex.test("42 42 42"));
|
||||
- text: Your regex should match <code>"100 100 100"</code>.
|
||||
testString: assert(reRegex.test("100 100 100"));
|
||||
- text: Your regex should not match <code>"42 42 42 42"</code>.
|
||||
testString: assert.equal(("42 42 42 42").match(reRegex.source), null);
|
||||
- text: Your regex should not match <code>"42 42"</code>.
|
||||
testString: assert.equal(("42 42").match(reRegex.source), null);
|
||||
- text: Your regex should not match <code>"101 102 103"</code>.
|
||||
testString: assert(!reRegex.test("101 102 103"));
|
||||
- text: Your regex should not match <code>"1 2 3"</code>.
|
||||
testString: assert(!reRegex.test("1 2 3"));
|
||||
- text: Your regex should match <code>"10 10 10"</code>.
|
||||
testString: assert(reRegex.test("10 10 10"));
|
||||
# --hints--
|
||||
|
||||
Your regex should use the shorthand character class for digits.
|
||||
|
||||
```js
|
||||
assert(reRegex.source.match(/\\d/));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should reuse a capture group twice.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
Your regex should have two spaces separating the three numbers.
|
||||
|
||||
```js
|
||||
assert(
|
||||
reRegex.source.match(/ |\\s/g).length === 2 ||
|
||||
reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g)
|
||||
);
|
||||
```
|
||||
|
||||
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";
|
||||
@ -68,19 +103,10 @@ let reRegex = /change/; // Change this line
|
||||
let result = reRegex.test(repeatNum);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let repeatNum = "42 42 42";
|
||||
let reRegex = /^(\d+)\s\1\s\1$/;
|
||||
let result = reRegex.test(repeatNum);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301365
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>"hah"</code> with the letter <code>a</code> <code>3</code> times, your regex would be <code>/ha{3}h/</code>.
|
||||
|
||||
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";
|
||||
@ -21,39 +23,56 @@ multipleHA.test(A3); // Returns true
|
||||
multipleHA.test(A100); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the regex <code>timRegex</code> to match the word <code>"Timber"</code> only when it has four letter <code>m</code>'s.
|
||||
</section>
|
||||
Change the regex `timRegex` to match the word `"Timber"` only when it has four letter `m`'s.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use curly brackets.
|
||||
testString: assert(timRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: Your regex should not match <code>"Timber"</code>
|
||||
testString: timRegex.lastIndex = 0; assert(!timRegex.test("Timber"));
|
||||
- text: Your regex should not match <code>"Timmber"</code>
|
||||
testString: timRegex.lastIndex = 0; assert(!timRegex.test("Timmber"));
|
||||
- text: Your regex should not match <code>"Timmmber"</code>
|
||||
testString: timRegex.lastIndex = 0; assert(!timRegex.test("Timmmber"));
|
||||
- text: Your regex should match <code>"Timmmmber"</code>
|
||||
testString: timRegex.lastIndex = 0; assert(timRegex.test("Timmmmber"));
|
||||
- text: Your regex should not match <code>"Timber"</code> with 30 <code>m</code>'s in it.
|
||||
testString: timRegex.lastIndex = 0; assert(!timRegex.test("Ti" + "m".repeat(30) + "ber"));
|
||||
Your regex should use curly brackets.
|
||||
|
||||
```js
|
||||
assert(timRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should not match `"Timber"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Timber'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -61,19 +80,10 @@ let timRegex = /change/; // Change this line
|
||||
let result = timRegex.test(timStr);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let timStr = "Timmmmber";
|
||||
let timRegex = /Tim{4}ber/; // Change this line
|
||||
let result = timRegex.test(timStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301366
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>"hah"</code> with the letter <code>a</code> appearing at least <code>3</code> times, your regex would be <code>/ha{3,}h/</code>.
|
||||
|
||||
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";
|
||||
@ -21,41 +23,57 @@ multipleA.test(A2); // Returns false
|
||||
multipleA.test(A100); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the regex <code>haRegex</code> to match the word <code>"Hazzah"</code> only when it has four or more letter <code>z</code>'s.
|
||||
</section>
|
||||
Change the regex `haRegex` to match the word `"Hazzah"` only when it has four or more letter `z`'s.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use curly brackets.
|
||||
testString: assert(haRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: Your regex should not match <code>"Hazzah"</code>
|
||||
testString: assert(!haRegex.test("Hazzah"));
|
||||
- text: Your regex should not match <code>"Hazzzah"</code>
|
||||
testString: assert(!haRegex.test("Hazzzah"));
|
||||
- text: Your regex should match <code>"Hazzzzah"</code>
|
||||
testString: assert("Hazzzzah".match(haRegex)[0].length === 8);
|
||||
- text: Your regex should match <code>"Hazzzzzah"</code>
|
||||
testString: assert("Hazzzzzah".match(haRegex)[0].length === 9);
|
||||
- text: Your regex should match <code>"Hazzzzzzah"</code>
|
||||
testString: assert("Hazzzzzzah".match(haRegex)[0].length === 10);
|
||||
- text: Your regex should match <code>"Hazzah"</code> with 30 <code>z</code>'s in it.
|
||||
testString: assert("Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah".match(haRegex)[0].length === 34);
|
||||
Your regex should use curly brackets.
|
||||
|
||||
```js
|
||||
assert(haRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should not match `"Hazzah"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!haRegex.test('Hazzah'));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -63,19 +81,10 @@ let haRegex = /change/; // Change this line
|
||||
let result = haRegex.test(haStr);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let haStr = "Hazzzzah";
|
||||
let haRegex = /Haz{4,}ah/; // Change this line
|
||||
let result = haRegex.test(haStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301367
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Recall that you use the plus sign <code>+</code> to look for one or more characters and the asterisk <code>*</code> 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 (<code>{</code> and <code>}</code>). You put two numbers between the curly brackets - for the lower and upper number of patterns.
|
||||
For example, to match only the letter <code>a</code> appearing between <code>3</code> and <code>5</code> times in the string <code>"ah"</code>, your regex would be <code>/a{3,5}h/</code>.
|
||||
# --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";
|
||||
@ -19,40 +21,57 @@ multipleA.test(A4); // Returns true
|
||||
multipleA.test(A2); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the regex <code>ohRegex</code> to match the entire phrase <code>"Oh no"</code> only when it has <code>3</code> to <code>6</code> letter <code>h</code>'s.
|
||||
</section>
|
||||
Change the regex `ohRegex` to match the entire phrase `"Oh no"` only when it has `3` to `6` letter `h`'s.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Your regex should use curly brackets.
|
||||
testString: assert(ohRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: Your regex should not match <code>"Ohh no"</code>
|
||||
testString: assert(!ohRegex.test("Ohh no"));
|
||||
- text: Your regex should match <code>"Ohhh no"</code>
|
||||
testString: assert("Ohhh no".match(ohRegex)[0].length === 7);
|
||||
- text: Your regex should match <code>"Ohhhh no"</code>
|
||||
testString: assert("Ohhhh no".match(ohRegex)[0].length === 8);
|
||||
- text: Your regex should match <code>"Ohhhhh no"</code>
|
||||
testString: assert("Ohhhhh no".match(ohRegex)[0].length === 9);
|
||||
- text: Your regex should match <code>"Ohhhhhh no"</code>
|
||||
testString: assert("Ohhhhhh no".match(ohRegex)[0].length === 10);
|
||||
- text: Your regex should not match <code>"Ohhhhhhh no"</code>
|
||||
testString: assert(!ohRegex.test("Ohhhhhhh no"));
|
||||
Your regex should use curly brackets.
|
||||
|
||||
```js
|
||||
assert(ohRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should not match `"Ohh no"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
```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";
|
||||
@ -60,16 +79,10 @@ let ohRegex = /change/; // Change this line
|
||||
let result = ohRegex.test(ohStr);
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let ohStr = "Ohhh no";
|
||||
let ohRegex = /Oh{3,6} no/; // Change this line
|
||||
let result = ohRegex.test(ohStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,10 +5,11 @@ challengeType: 1
|
||||
forumTopicId: 301368
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>.replace()</code> on a string. The inputs for <code>.replace()</code> 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.
|
||||
|
||||
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.";
|
||||
@ -17,47 +18,55 @@ wrongText.replace(silverRegex, "blue");
|
||||
// Returns "The sky is blue."
|
||||
```
|
||||
|
||||
You can also access capture groups in the replacement string with dollar signs (<code>$</code>).
|
||||
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"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a regex <code>fixRegex</code> using three capture groups that will search for each word in the string "one two three". Then update the <code>replaceText</code> variable to replace "one two three" with the string "three two one" and assign the result to the <code>result</code> variable. Make sure you are utilizing capture groups in the replacement string using the dollar sign (<code>$</code>) syntax.
|
||||
</section>
|
||||
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.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should use <code>.replace()</code> to search and replace.
|
||||
testString: assert(code.match(/\.replace\(.*\)/));
|
||||
- text: Your regex should change <code>"one two three"</code> to <code>"three two one"</code>
|
||||
testString: assert(result === "three two one");
|
||||
- text: You should not change the last line.
|
||||
testString: assert(code.match(/result\s*=\s*str\.replace\(.*?\)/));
|
||||
- text: <code>fixRegex</code> should use at least three capture groups.
|
||||
testString: assert((new RegExp(fixRegex.source + '|')).exec('').length - 1 >= 3);
|
||||
- text: <code>replaceText</code> should use parenthesized submatch string(s) (i.e. the nth parenthesized submatch string, $n, corresponds to the nth capture group).
|
||||
testString: '{
|
||||
const re = /(\$\d{1,2})+(?:[\D]|\b)/g;
|
||||
assert(replaceText.match(re).length >= 3);
|
||||
}'
|
||||
You should use `.replace()` to search and replace.
|
||||
|
||||
```js
|
||||
assert(code.match(/\.replace\(.*\)/));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your regex should change `"one two three"` to `"three two one"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(result === 'three two one');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
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";
|
||||
@ -66,14 +75,7 @@ let replaceText = ""; // Change this line
|
||||
let result = str.replace(fixRegex, replaceText);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let str = "one two three";
|
||||
@ -81,5 +83,3 @@ let fixRegex = /(\w+) (\w+) (\w+)/g; // Change this line
|
||||
let replaceText = "$3 $2 $1"; // Change this line
|
||||
let result = str.replace(fixRegex, replaceText);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,11 +5,13 @@ challengeType: 1
|
||||
forumTopicId: 301369
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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 <code>"the"</code> in the string <code>"The dog chased the cat"</code>, you could use the following regular expression: <code>/the/</code>. 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 <code>.test()</code> method. The <code>.test()</code> method takes the regex, applies it to a string (which is placed inside the parentheses), and returns <code>true</code> or <code>false</code> if your pattern finds something or not.
|
||||
|
||||
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";
|
||||
@ -18,31 +20,27 @@ testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Apply the regex <code>myRegex</code> on the string <code>myString</code> using the <code>.test()</code> method.
|
||||
</section>
|
||||
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should use <code>.test()</code> to test the regex.
|
||||
testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
- text: Your result should return <code>true</code>.
|
||||
testString: assert(result === true);
|
||||
You should use `.test()` to test the regex.
|
||||
|
||||
```js
|
||||
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
```
|
||||
|
||||
</section>
|
||||
Your result should return `true`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(result === true);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let myString = "Hello, World!";
|
||||
@ -50,19 +48,10 @@ let myRegex = /Hello/;
|
||||
let result = myRegex; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myString = "Hello, World!";
|
||||
let myRegex = /Hello/;
|
||||
let result = myRegex.test(myString); // Change this line
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user