fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
This commit is contained in:
@@ -1,21 +1,32 @@
|
||||
---
|
||||
title: Check for All or None
|
||||
---
|
||||
## Check for All or None
|
||||
|
||||
# Check for All or None
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
In this challenge, you are asked to change the regex so that it defines the existence of the letter u, resulting in the acceptance of both American and British versions of the word favourite/favorite.
|
||||
|
||||
### Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
The variable favWord already contains the American version — favorite. Does the regular expression in favRegex also match the British version?
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Have you used the question mark, ? symbol, if so, have you placed it in the correct position?
|
||||
|
||||
### Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let favWord = "favorite";
|
||||
let favRegex = /favou?rite/; // Change this line
|
||||
let result = favRegex.test(favWord);
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: Check For Mixed Grouping of Characters
|
||||
---
|
||||
## Check For Mixed Grouping of Characters
|
||||
# Check For Mixed Grouping of Characters
|
||||
|
||||
### Hint 1
|
||||
|
||||
@@ -15,7 +15,9 @@ Your regex should use mixed grouping like `/P(engu|umpk)in/g`.
|
||||
|
||||
Use `.*` to allow for middle names.
|
||||
|
||||
### Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
```javascript
|
||||
let myString = "Eleanor Roosevelt";
|
||||
|
@@ -1,25 +1,34 @@
|
||||
---
|
||||
title: Extract Matches
|
||||
---
|
||||
## Extract Matches
|
||||
# Extract Matches
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
---
|
||||
## Problem Explanation
|
||||
Using the `match()` method, you can extract parts of a string that match with your regular expression. In this challenge, you are extracting the word "coding" from the string provided.
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Change your regex to detect the word "coding".
|
||||
|
||||
## Hint 2:
|
||||
### Hint 2
|
||||
|
||||
Did you call the `match()` method on the string?
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let extractStr = "Extract the word 'coding' from this string.";
|
||||
let codingRegex = /coding/;
|
||||
let result = extractStr.match(codingRegex);
|
||||
let codingRegex = /coding/;
|
||||
let result = extractStr.match(codingRegex);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,15 +1,22 @@
|
||||
---
|
||||
title: Find Characters with Lazy Matching
|
||||
---
|
||||
## Find Characters with Lazy Matching
|
||||
# Find Characters with Lazy Matching
|
||||
|
||||
#### Challange:
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
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.
|
||||
|
||||
#### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
let text = "<h1>Winter is coming</h1>";
|
||||
let myRegex = /<h1>?/; // it's the answer!
|
||||
let result = text.match(myRegex);
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,24 +1,34 @@
|
||||
---
|
||||
title: Find More Than the First Match
|
||||
---
|
||||
## Find More Than the First Match
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
# Find More Than the First Match
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
If you have multiple occurrences of your regex inside a string, you can get the `match()` function to detect all of them. Simply tag along the `g` flag at the end of your regex! That's what you're doing in this challenge.
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Change the regex so that it detects the word "twinkle".
|
||||
|
||||
## Hint 2:
|
||||
### Hint 2
|
||||
|
||||
You can add multiple tags to a regex! For example, a regex that detects multiple occurrences, and detects regardless of case, can be structured like `gi` or `ig`.
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let twinkleStar = "Twinkle, twinkle, little star";
|
||||
let starRegex = /twinkle/gi;
|
||||
let result = twinkleStar.match(starRegex);
|
||||
let starRegex = /twinkle/gi;
|
||||
let result = twinkleStar.match(starRegex);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,34 +1,42 @@
|
||||
---
|
||||
title: Find One or More Criminals in a Hunt
|
||||
---
|
||||
## Find One or More Criminals in a Hunt
|
||||
# Find One or More Criminals in a Hunt
|
||||
|
||||
## The Problem
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
### Hint 1:
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Are you surrounding your regexp in slashes?
|
||||
```javascript
|
||||
let regexp = /t.[a-z]*t/;
|
||||
```
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Are you using the '+' flag in your regexp?
|
||||
```javascript
|
||||
let regexp = /E+/; // returns E, EE, EEE patterns
|
||||
```
|
||||
|
||||
### Spoiler Warning - Solution Ahead
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let crowd = 'P1P2P3P4P5P6CCCP7P8P9';
|
||||
let crowd = "P1P2P3P4P5P6CCCP7P8P9";
|
||||
|
||||
let reCriminals = /C+/; // Change this line
|
||||
|
||||
let matchedCriminals = crowd.match(reCriminals);
|
||||
console.log(matchedCriminals);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,21 +1,29 @@
|
||||
---
|
||||
title: Ignore Case While Matching
|
||||
---
|
||||
## Ignore Case While Matching
|
||||
# Ignore Case While Matching
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
---
|
||||
## Problem Explanation
|
||||
When creating a regular expression, you might want to match parts of string that are same in spelling, but different in case. To do this, you add the `i` flag to the end of the regex. In this challenge, you are doing just that.
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Modify the regex so that it detects "freeCodeCamp", "FREECODECAMP", and "FrEeCoDeCaMp". Maybe using the `i` flag might help?
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let myString = "freeCodeCamp";
|
||||
let fccRegex = /freeCodeCamp/i;
|
||||
let fccRegex = /freeCodeCamp/i;
|
||||
let result = fccRegex.test(myString);
|
||||
```
|
||||
</details>
|
@@ -1,13 +1,11 @@
|
||||
---
|
||||
title: Regular Expressions
|
||||
---
|
||||
## Regular Expressions
|
||||
# Regular Expressions
|
||||
|
||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/mathematics/quadratic-equations/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
||||
|
||||
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
|
||||
#### More Information:
|
||||
<!-- Please add any articles you think might be helpful to read before writing the article -->
|
||||
|
@@ -1,21 +1,30 @@
|
||||
---
|
||||
title: Match a Literal String with Different Possibilities
|
||||
---
|
||||
## Match a Literal String with Different Possibilities
|
||||
# Match a Literal String with Different Possibilities
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
---
|
||||
## Problem Explanation
|
||||
Suppose you want to match many different words with your regular expression; using the `|` symbol, that becomes possible. In this challenge, you are using that symbol to identify four different pets hidden within strings!
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Inside the string literal, place the pet names, each seperated by the `|` symbol.
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
let petString = "James has a pet cat.";
|
||||
let petRegex = /dog|cat|bird|fish/;
|
||||
let result = petRegex.test(petString);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,17 +1,20 @@
|
||||
---
|
||||
title: Match All Letters and Numbers
|
||||
---
|
||||
## The Problem
|
||||
|
||||
Use the shorthand character class \w to count the number of alphanumeric characters in various quotes and strings.
|
||||
# Match All Letters and Numbers
|
||||
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let alphabetRegexV2 = /\w/gi; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let alphabetRegexV2 = /\w/gi; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
```
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
</details>
|
||||
|
@@ -1,24 +1,31 @@
|
||||
---
|
||||
title: Match All Non-Numbers
|
||||
---
|
||||
## Match All Non-Numbers
|
||||
# Match All Non-Numbers
|
||||
|
||||
## Hint 1
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
* You should try using a global flag.
|
||||
|
||||
## Hint 2
|
||||
### Hint 2
|
||||
|
||||
* Try a shorthand character for non-digits characters.
|
||||
|
||||
# Spoiler Alert!! Solution Ahead!
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let noNumRegex = /\D/g;
|
||||
```
|
||||
|
||||
## Explanation
|
||||
#### Code Explanation
|
||||
|
||||
* The `\D` shorthand character is used to match non-digits characters, it has the same result as using `[^0-9]`;
|
||||
</details>
|
||||
|
@@ -1,24 +1,32 @@
|
||||
---
|
||||
title: Match All Numbers
|
||||
---
|
||||
## Match All Numbers
|
||||
# Match All Numbers
|
||||
|
||||
## Hint 1
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
* A global flag will help you get through this challenge.
|
||||
|
||||
## Hint 2
|
||||
### Hint 2
|
||||
|
||||
* Try using a shorthand character for `d`igits.
|
||||
|
||||
# Spoiler Alert!! Solution Ahead!
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let numRegex = /\d/g;
|
||||
```
|
||||
|
||||
## Explanation
|
||||
#### Code Explanation
|
||||
|
||||
* The `\d` shorthand character is a shortcut for `[0-9]`, it search for any number between 0 and 9.
|
||||
* The `\d` shorthand character is a shortcut for `[0-9]`, it search for any number between 0 and 9.
|
||||
</details>
|
@@ -1,21 +1,31 @@
|
||||
---
|
||||
title: Match Anything with Wildcard Period
|
||||
---
|
||||
## Match Anything with Wildcard Period
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
|
||||
## Hint 1:
|
||||
# Match Anything with Wildcard Period
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
The dot `.` is the key in this challenge.
|
||||
|
||||
## Hint 2:
|
||||
### Hint 2
|
||||
You should put the dot on the right position.
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let exampleStr = "Let's have fun with regular expressions!";
|
||||
let unRegex = /.un/; // Change this line
|
||||
let result = unRegex.test(exampleStr);
|
||||
```
|
||||
## Explaination
|
||||
#### Code Explanation
|
||||
The period `.` will be any one character so the strings "run", "sun", "fun" and "pun" have the same un charaters.
|
||||
</details>
|
||||
|
||||
|
@@ -1,26 +1,37 @@
|
||||
---
|
||||
title: Match Beginning String Patterns
|
||||
---
|
||||
## Match Beginning String Patterns
|
||||
# Match Beginning String Patterns
|
||||
|
||||
## The Problem
|
||||
---
|
||||
## Problem Explanation
|
||||
Use the caret character in a regex to find "Cal" only in the beginning of the string rickyAndCal.
|
||||
|
||||
### Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Try surrounding your regexp in slashes
|
||||
```javascript
|
||||
let testExp = /^test/;
|
||||
let testExp = /^test/;
|
||||
// returns true or false depending on whether test is found in the beginning of the string
|
||||
```
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Try using the '^' character caret outside of brackets as seen in the above example
|
||||
|
||||
### Spoiler Alert - Solution Ahead
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let rickyAndCal = "Cal and Ricky both like racing.";
|
||||
let calRegex = /^Cal/; // Change this line
|
||||
let result = calRegex.test(rickyAndCal);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,12 +1,21 @@
|
||||
---
|
||||
title: Match Characters that Occur One or More Times
|
||||
---
|
||||
## Match Characters that Occur One or More Times
|
||||
# Match Characters that Occur One or More Times
|
||||
|
||||
## the problem:
|
||||
You want to find matches when the letter s occurs one or more times in "Mississippi". Write a regex that uses the + sign.
|
||||
## the solution
|
||||
---
|
||||
## Problem Explanation
|
||||
You want to find matches when the letter s occurs one or more times in "Mississippi". Write a regex that uses the + sign.
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let difficultSpelling = "Mississippi";
|
||||
let myRegex = /s+/g; // this is the solution
|
||||
let result = difficultSpelling.match(myRegex);
|
||||
```
|
||||
</details>
|
@@ -2,8 +2,10 @@
|
||||
title: Match Characters that Occur Zero or More Times
|
||||
---
|
||||
|
||||
## Match Characters that Occur Zero or More Times
|
||||
# Match Characters that Occur Zero or More Times
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
Any letter in a regex expression that is followed by a `*` does not have to occur in the string tested whereas any letter in a regex expression followed by a `+` must occur in a string at least once, as shown below,
|
||||
|
||||
```javascript
|
||||
@@ -12,8 +14,8 @@ let phrase = "ba humbug";
|
||||
let regexPlus = /bah+/;
|
||||
let regexStar = /bah*/;
|
||||
|
||||
regexPlus.test(phrase); // returns false
|
||||
regexStar.test(phrase); // returns true
|
||||
regexPlus.test(phrase); // returns false
|
||||
regexStar.test(phrase); // returns true
|
||||
```
|
||||
|
||||
Both allow for any number of occurrences of the same letter in a row, for example,
|
||||
@@ -28,11 +30,16 @@ regexPlus.test(phrase); // returns true
|
||||
regexStar.test(phrase); // returns true
|
||||
```
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
|
||||
let chewieRegex = /Aa*/; // Change this line
|
||||
let result = chewieQuote.match(chewieRegex);
|
||||
```
|
||||
</details>
|
||||
|
||||
|
@@ -1,9 +1,10 @@
|
||||
---
|
||||
title: Match Ending String Patterns
|
||||
---
|
||||
## Match Ending String Patterns
|
||||
|
||||
# Match Ending String Patterns
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
To finish this challenge, it's necessary to use __boundaries__.
|
||||
|
||||
The __$__ Matches end of input.
|
||||
@@ -15,9 +16,14 @@ __important:__ If the multiline flag is set to true, also matches immediately be
|
||||
|
||||
|
||||
|
||||
### Spoiiler Alert: Solution ahead
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let caboose = "The last car on a train is the caboose";
|
||||
let lastRegex = /caboose$/; // Change this line
|
||||
let result = lastRegex.test(caboose);
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,22 +1,27 @@
|
||||
---
|
||||
title: Match Everything But Letters and Numbers
|
||||
---
|
||||
## Match Everything But Letters and Numbers
|
||||
|
||||
# Match Everything But Letters and Numbers
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
To finish this challenge, it's necessary to use __character classes__.
|
||||
|
||||
The __\W__ matches any character that is not a word character from the basic Latin alphabet.
|
||||
__Equivalent to [^A-Za-z0-9_].__
|
||||
|
||||
For example, /\W/ or /[^A-Za-z0-9_]/ matches "%" in "50%".
|
||||
For example, `/\W/` or `/[^A-Za-z0-9_]/` matches "%" in "50%".
|
||||
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
### Spoiiler Alert: Solution ahead
|
||||
```javascript
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let nonAlphabetRegex = /\W/g; // Change this line
|
||||
let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,20 +1,31 @@
|
||||
---
|
||||
title: Match Letters of the Alphabet
|
||||
---
|
||||
## Match Letters of the Alphabet
|
||||
# Match Letters of the Alphabet
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
In this challenge, you're asked to match all of the letters of the alphabet within a given string. Not only are you matching/searching for these characters, but you're asked to extract them.
|
||||
|
||||
### Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Remember that you're asked to extract the letters from the string -- this cannot be done with the .test() method because it returns True or False. In this case, we need to extract the actual result from the string using the .match() method.
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Are you using the match() method character case flag with brackets? e.g. regExp = /[a-e]/ vs regExp = /a-e/. What this allows us to do is search through the string for any characters matching [a, b, c, ... e] using the shorthand notation /[a-e]/.
|
||||
|
||||
### Spoiler alert: Solution Ahead
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let quoteSample = "The quick brown fox jumps over the lazy dog.";
|
||||
let alphabetRegex = /[a-z]/ig; // Change this line
|
||||
let alphabetRegex = /[a-z]/gi; // Change this line
|
||||
let result = quoteSample.match(alphabetRegex); // Change this line
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,23 +1,33 @@
|
||||
---
|
||||
title: Match Literal Strings
|
||||
---
|
||||
## Match Literal Strings
|
||||
# Match Literal Strings
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
---
|
||||
## Problem Explanation
|
||||
This challenge is not any different from the previous; in this case though, you are learning that string literals are case-sensitive. That means, when you test to see if a string has a literal, it will search for the exact case (lower or upper) inside the string. You will learn how to find string literals regardless of their case, in an upcoming lesson.
|
||||
|
||||
In this challenge, you're finding Waldo...inside a string!
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Change the line to have the correct string literal.
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
|
||||
```javascript
|
||||
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
||||
let waldoRegex = /Waldo/; // Change this line
|
||||
let result = waldoRegex.test(waldoIsHiding);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,26 +1,34 @@
|
||||
---
|
||||
title: Match Non-Whitespace Characters
|
||||
---
|
||||
## Match Non-Whitespace Characters
|
||||
# Match Non-Whitespace Characters
|
||||
|
||||
## Hint 1
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
* A global flag will help you get through this challenge.
|
||||
|
||||
## Hint 2
|
||||
### Hint 2
|
||||
|
||||
* Try using a shorthand character for `S` non-whitespace.
|
||||
|
||||
# Spoiler Alert!! Solution Ahead!
|
||||
|
||||
## Solution
|
||||
|
||||
```
|
||||
javascript
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countNonWhiteSpace = /\S/g; // Change this line
|
||||
let result = sample.match(countNonWhiteSpace);
|
||||
```
|
||||
|
||||
## Explanation
|
||||
#### Code Explanation
|
||||
|
||||
* The `\S` shorthand character is a shortcut for non-whitespace. The regular expresssion returns the number of characters that match it.
|
||||
</details>
|
||||
|
@@ -1,32 +1,45 @@
|
||||
---
|
||||
title: Match Numbers and Letters of the Alphabet
|
||||
---
|
||||
## Match Numbers and Letters of the Alphabet
|
||||
|
||||
# Match Numbers and Letters of the Alphabet
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
In this challenge, you are asked to return a collection of both numbers and letters extracted from a string. Our goal is to create a single regexp that captures the range of letters between h and s, and the numbers from 2 to 6.
|
||||
|
||||
### Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Are you using the match() method? If so, then are you calling the method from the appropriate variable? i.e.
|
||||
```javascript
|
||||
let input_string = "The string you are testing on"
|
||||
let yourRegExp = /[h-s]/;
|
||||
let correct_result = input_string.match(yourRegExp); // passes - returns characters H to S
|
||||
|
||||
let incorrect_result = yourRegExp.match(input_string); // fails - .match() is not a function
|
||||
let input_string = "The string you are testing on";
|
||||
let yourRegExp = /[h-s]/;
|
||||
let correct_result = input_string.match(yourRegExp); // passes - returns characters H to S
|
||||
|
||||
let incorrect_result = yourRegExp.match(input_string); // fails - .match() is not a function
|
||||
```
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Did you remember to enable the regexp flags such as "i" for ignoring case and "g" for retreiving multiple values? If so, then are you including both the character case match for numbers AND letters?
|
||||
```javascript
|
||||
let regexp = /[a-z1-100]/ig
|
||||
let regexp = /[a-z1-100]/gi;
|
||||
// above code returns all characters from A to Z, along with all numbers from 1 to 100
|
||||
// this includes the letter A and Z and the numbers 1 and 100
|
||||
```
|
||||
|
||||
### Spoiler Alert - Solution Ahead
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let quoteSample = "Blueberry 3.141592653s are delicious.";
|
||||
let myRegex = /[h-s2-6]/ig; // Change this line
|
||||
let myRegex = /[h-s2-6]/gi; // Change this line
|
||||
let result = quoteSample.match(myRegex); // Change this line
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,22 +1,32 @@
|
||||
---
|
||||
title: Match Single Character with Multiple Possibilities
|
||||
---
|
||||
## Match Single Character with Multiple Possibilities
|
||||
# Match Single Character with Multiple Possibilities
|
||||
|
||||
### Extract
|
||||
---
|
||||
## Problem Explanation
|
||||
Using the match() method, you can extract parts of a string that match with your regular expression. In this challenge, you are extracting the vowels "a, e, i, o, u" from a provided string.
|
||||
|
||||
### Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Are you attempting to use the test() method? Remember this method only returns True or False -- we need to extract the vowels from the string.
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Have you tried using the '[]' character case match without commas? i.e. [abcd] vs [a, b, c, d]
|
||||
|
||||
### Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
|
||||
let vowelRegex = /[aeiou]/ig; // Change this line
|
||||
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
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,10 +1,18 @@
|
||||
---
|
||||
title: Match Single Characters Not Specified
|
||||
---
|
||||
## Match Single Characters Not Specified
|
||||
|
||||
# Match Single Characters Not Specified
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
In this challenge, we are asked to return a collection of matches that are not exactly specified. Whereas previous regexp challenges would have you match within the character case [a-z], this challenge instead asks us to negate these matches using the caret character [^a-z]. Our goal then is to return a negated collection (non-matches) of letters that are not vowels nor numbers.
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Did you remember to surround your regexp in both brackets and slashes?
|
||||
```javascript
|
||||
let exampleRegExp = /[^a-z]/;
|
||||
@@ -14,17 +22,21 @@ If so, then double check you're adding the appropriate flags:
|
||||
* g : Retrieves multiple values; default is set to return the first match it encounters
|
||||
* ^ : Negates the matches following this flag
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Be sure to check whether your number range is correct -- the challenge asks us to negate all numbers from 0 to 9. This can be done using the negate caret placed immediately after the first opening bracket of your regexp.
|
||||
```js
|
||||
let numbersRegExp = /[^0-9]/ig;
|
||||
let numbersRegExp = /[^0-9]/gi;
|
||||
```
|
||||
|
||||
### Spoiler Alert - Solution Ahead
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let quoteSample = "3 blind mice.";
|
||||
let myRegex = /[^aeiou^0-9]/ig; // Change this line
|
||||
let myRegex = /[^aeiou^0-9]/gi; // Change this line
|
||||
let result = quoteSample.match(myRegex); // Change this line
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,24 +1,33 @@
|
||||
---
|
||||
title: Match Whitespace
|
||||
---
|
||||
## Match Whitespace
|
||||
# Match Whitespace
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
To finish this challenge, it's necessary to use the __/s__ character class in your regexp pattern.
|
||||
|
||||
__\s__ matches a single white space character. (including space, tab, form feed, line feed and other Unicode spaces.
|
||||
|
||||
For example:
|
||||
```javascript
|
||||
/\s\w*/
|
||||
/\s\w*/;
|
||||
// matches " bar" in "foo bar".
|
||||
```
|
||||
|
||||
__important:__ Characters are case sensitive in regexp. __\S__ matches a single character other than white space.
|
||||
|
||||
|
||||
### Spoiiler Alert: Solution ahead
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countWhiteSpace = /\s/g; // Change this line
|
||||
let result = sample.match(countWhiteSpace);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,16 +1,27 @@
|
||||
---
|
||||
title: Positive and Negative Lookahead
|
||||
---
|
||||
## Positive and Negative Lookahead
|
||||
# Positive and Negative Lookahead
|
||||
|
||||
- Remeber to use 2 `lookaheads` to check the patterns in the string. The first `lookahead` is very similar to that given in the example - '(?=\w{3,6})' - only the `lower-number` 3 is too low for our test cases, and an `upper-number` is completely unneccesarry. This first `lookahead` is only used to find a string consisting of a certain amount of characters. A second `lookahead` must be used to check for consecutive numerical values at the end of the string.
|
||||
|
||||
- The second `lookahead` is also similar to that given in the example - `(?=\D*\d)` - however, this expression too must be modified to pass all test cases. Remember to specify the exact amount of numbers you want to appear at the end of the string.
|
||||
---
|
||||
## Hints
|
||||
|
||||
## Solution :
|
||||
### Hint 1
|
||||
Remeber to use 2 `lookaheads` to check the patterns in the string. The first `lookahead` is very similar to that given in the example - '(?=\w{3,6})' - only the `lower-number` 3 is too low for our test cases, and an `upper-number` is completely unneccesarry. This first `lookahead` is only used to find a string consisting of a certain amount of characters. A second `lookahead` must be used to check for consecutive numerical values at the end of the string.
|
||||
|
||||
### Hint 2
|
||||
The second `lookahead` is also similar to that given in the example - `(?=\D*\d)` - however, this expression too must be modified to pass all test cases. Remember to specify the exact amount of numbers you want to appear at the end of the string.
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let sampleWord = "astronaut";
|
||||
let pwRegex = /(?=\w{6,})(?=\D*\d{2})/;
|
||||
let result = pwRegex.test(sampleWord);
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,21 +1,32 @@
|
||||
---
|
||||
title: Remove Whitespace from Start and End
|
||||
---
|
||||
## Remove Whitespace from Start and End
|
||||
# Remove Whitespace from Start and End
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
To solve this challenge you simply have to create a regex string that matches any spaces at the beginning or at the end of the string.
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Think of how you can select substrings at the beginning or end of a string.
|
||||
|
||||
## Hint 2:
|
||||
### Hint 2
|
||||
Think of how you can select whitespace
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let hello = " Hello, World! ";
|
||||
let wsRegex = /^\s+|\s+$/g; // Change this line
|
||||
let result = hello.replace(wsRegex, ''); // Change this line
|
||||
let result = hello.replace(wsRegex, ""); // Change this line
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,15 +1,20 @@
|
||||
---
|
||||
title: Restrict Possible Usernames
|
||||
---
|
||||
## Restrict Possible Usernames
|
||||
# Restrict Possible Usernames
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
## Solution:
|
||||
```javascript
|
||||
let username = "JackOfAllTrades";
|
||||
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
|
||||
let result = userCheck.test(username);
|
||||
```
|
||||
## Explain:
|
||||
#### Code Explanation
|
||||
1. `^` - start of input
|
||||
2. `[a-z]` - first character is a letter
|
||||
3. `[0-9]{2,0}` - ends with two or more numbers
|
||||
@@ -18,3 +23,4 @@ let result = userCheck.test(username);
|
||||
6. `\d*` - and ends with zero or more numbers
|
||||
7. `$` - end of input
|
||||
8. `i` - ignore case of input
|
||||
</details>
|
||||
|
@@ -1,13 +1,17 @@
|
||||
---
|
||||
title: Reuse Patterns Using Capture Groups
|
||||
---
|
||||
## Reuse Patterns Using Capture Group
|
||||
# Reuse Patterns Using Capture Group
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Given code below:
|
||||
```javascript
|
||||
let testString = "test test test";
|
||||
let reRegex =/(test)\s\1/;
|
||||
let reRegex = /(test)\s\1/;
|
||||
let result = reRegex.test(testString);
|
||||
```
|
||||
`result` will match only `test test` because `\1` in this example stands for the same text as most recently matched by the 1st capturing group `(test)`.
|
||||
@@ -20,32 +24,36 @@ let literalRe = /test\stest/;
|
||||
```
|
||||
Both `re` and `literalRe` would match the same thing.
|
||||
|
||||
## Hint 2:
|
||||
### Hint 2
|
||||
Given the code below:
|
||||
```javascript
|
||||
let testString = "test test test";
|
||||
let reRegex =/(test)(\s)\1\2\1/;
|
||||
let reRegex = /(test)(\s)\1\2\1/;
|
||||
let result = reRegex.test(testString);
|
||||
```
|
||||
`result` will match whole `test test test` because:
|
||||
`\1` repeats (test)
|
||||
`\2` repeats (\s)
|
||||
|
||||
## Hint 3:
|
||||
### Hint 3
|
||||
The code below:
|
||||
```javascript
|
||||
let testString = "test test test test test test";
|
||||
let reRegex =/(test)(\s)\1\2\1/g;
|
||||
let reRegex = /(test)(\s)\1\2\1/g;
|
||||
let result = reRegex.test(testString);
|
||||
```
|
||||
because we used `\g`, our Regex doesn't return after first full match (`test test test`) and matched all repetitions.
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let repeatNum = "42 42 42";
|
||||
let reRegex = /^(\d+)\s\1\s\1$/;
|
||||
let reRegex = /^(\d+)\s\1\s\1$/;
|
||||
let result = reRegex.test(repeatNum);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,28 +1,37 @@
|
||||
---
|
||||
title: Specify Exact Number of Matches
|
||||
---
|
||||
## Specify Exact Number of Matches
|
||||
# Specify Exact Number of Matches
|
||||
|
||||
### Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
We see that the answer is simply finding 4 m's in a row. The first anwser would be to match EXACTLY 4 times the character so we shall implement as following:
|
||||
````javascript
|
||||
let timRegex = /m{4}/;
|
||||
````
|
||||
```javascript
|
||||
let timRegex = /m{4}/;
|
||||
```
|
||||
This solution is incorrect because you wont pass the final test case ("Timber" with 30 m's in it) as there are multiple times mmmm in a row of 30 m.
|
||||
You should try to get **no more than** ***(4 times m) mmmm***.
|
||||
|
||||
### Hint 2:
|
||||
### Hint 2
|
||||
Based on the above I will try to specify exactly the characters before and after 4 times m!
|
||||
***Remember:*** e.g. /b/ is a string literal for the word b so what if before and after mmmm we add ALL missing letters?
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
### Solution
|
||||
From hint 1 we defined how to find mmmm in the timStr. From hint 2 we observe that we need to find the word Timmmmber=Tim{4}ber.
|
||||
We simpply add the exact word in our timRegex value and the final result is:
|
||||
````javascript
|
||||
|
||||
```javascript
|
||||
let timStr = "Timmmmber";
|
||||
let timRegex = /Tim{4}ber/; // Change this line
|
||||
let result = timRegex.test(timStr);
|
||||
````
|
||||
```
|
||||
</details>
|
||||
|
||||
|
@@ -1,14 +1,17 @@
|
||||
---
|
||||
title: Specify Only the Lower Number of Matches
|
||||
---
|
||||
## Specify Only the Lower Number of Matches
|
||||
# Specify Only the Lower Number of Matches
|
||||
|
||||
The Problem
|
||||
Change the regex haRegex to match the word "Hazzah" only when it has four or more letter z's.
|
||||
|
||||
Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
let haStr = "Hazzzzah";
|
||||
let haRegex = /Haz{4,}ah/; // Change this line
|
||||
let result = haRegex.test(haStr);
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,8 +1,10 @@
|
||||
---
|
||||
title: Specify Upper and Lower Number of Matches
|
||||
---
|
||||
## Specify Upper and Lower Number of Matches
|
||||
# Specify Upper and Lower Number of Matches
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
Remember `/a{2,4}/` will return `true` as long as there are between two to four a's together. It will return `true` for any string that has more than four a's together as well.
|
||||
|
||||
All these strings will return `true`:
|
||||
@@ -12,17 +14,27 @@ let fourAs = "aaaa";
|
||||
let sevenAs = "aaaaaaa";
|
||||
|
||||
let myRegex = /a{2,4}/;
|
||||
myRegex.test(threeAs) ; // true
|
||||
myRegex.test(fourAs) ; // true
|
||||
myRegex.test(sevenAs) ; // true
|
||||
myRegex.test(threeAs); // true
|
||||
myRegex.test(fourAs); // true
|
||||
myRegex.test(sevenAs); // true
|
||||
```
|
||||
## Spoiler Alert!
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
Remember to use `\s` after `Oh{3,6}` to include a white space, followed by `no` to pass all test cases. All test cases are written using a capital O, however the testcases could also be passed by using `ignore-case` : `/oh{3,6}\sno/i`
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let ohStr = "Ohhh no";
|
||||
let ohRegex = /Oh{3,6}\sno/; // Change this line
|
||||
let result = ohRegex.test(ohStr);
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,18 +1,25 @@
|
||||
---
|
||||
title: Use Capture Groups to Search and Replace
|
||||
---
|
||||
## Use Capture Groups to Search and Replace
|
||||
# Use Capture Groups to Search and Replace
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
---
|
||||
## Problem Explanation
|
||||
Using `.replace()` with the first parameter set to find the part of the original string to replace, and the second parameter should be the replacement.
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Modify the regex so that `fixRegex` detects the part of the string to replace and the variable `replaceText` should be modified to the string that will replace `fixRegex`.
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let huhText = "This sandwich is good.";
|
||||
@@ -20,3 +27,4 @@ let fixRegex = /good/; // Change this line
|
||||
let replaceText = "okey-dokey"; // Change this line
|
||||
let result = huhText.replace(fixRegex, replaceText);
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,21 +1,29 @@
|
||||
---
|
||||
title: Using the Test Method
|
||||
---
|
||||
## Using the Test Method
|
||||
# Using the Test Method
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
---
|
||||
## Problem Explanation
|
||||
In this challenge, you are checking to see if a string contains a certain "regular expression", or **regex** (**reg**ular **ex**pressions). You will use the `test()` method to do that.
|
||||
|
||||
## Hint 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Call the test method on `myRegex`. What do you think the argument will be?
|
||||
|
||||
## Spoiler Alert - Solution Ahead!
|
||||
|
||||
## Solution
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let myString = "Hello, World!";
|
||||
let myRegex = /Hello/;
|
||||
let result = myRegex.test(myString); // Change this line
|
||||
```
|
||||
</details>
|
||||
|
Reference in New Issue
Block a user