fix(curriculum): Convert blockquote elements to triple backtick syntax for JavaScript Algorithms and Data Structures (#35992)
* fix: convert js algorithms and data structures * fix: revert some blocks back to blockquote * fix: reverted comparison code block to blockquotes * fix: change js to json Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * fix: convert various section to triple backticks * fix: Make the formatting consistent for comparisons
This commit is contained in:
@@ -9,7 +9,15 @@ challengeType: 1
|
||||
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.
|
||||
For example, there are slight differences in American and British English and you can use the question mark to match both spellings.
|
||||
<blockquote>let american = "color";<br>let british = "colour";<br>let rainbowRegex= /colou?r/;<br>rainbowRegex.test(american); // Returns true<br>rainbowRegex.test(british); // Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let american = "color";
|
||||
let british = "colour";
|
||||
let rainbowRegex= /colou?r/;
|
||||
rainbowRegex.test(american); // Returns true
|
||||
rainbowRegex.test(british); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,7 +9,14 @@ challengeType: 1
|
||||
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.
|
||||
<blockquote>let testStr = "Pumpkin";<br>let testRegex = /P(engu|umpk)in/g;<br>testRegex.test(testStr);<br>// Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let testStr = "Pumpkin";
|
||||
let testRegex = /P(engu|umpk)in/g;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,7 +8,16 @@ challengeType: 1
|
||||
<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. Here's an example:
|
||||
<blockquote>"Hello, World!".match(/Hello/);<br>// Returns ["Hello"]<br>let ourStr = "Regular expressions";<br>let ourRegex = /expressions/;<br>ourStr.match(ourRegex);<br>// Returns ["expressions"]</blockquote>
|
||||
|
||||
```js
|
||||
"Hello, World!".match(/Hello/);
|
||||
// Returns ["Hello"]
|
||||
let ourStr = "Regular expressions";
|
||||
let ourRegex = /expressions/;
|
||||
ourStr.match(ourRegex);
|
||||
// Returns ["expressions"]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -7,9 +7,22 @@ challengeType: 1
|
||||
## Description
|
||||
<section id='description'>
|
||||
So far, you have only been able to extract or search a pattern once.
|
||||
<blockquote>let testStr = "Repeat, Repeat, Repeat";<br>let ourRegex = /Repeat/;<br>testStr.match(ourRegex);<br>// Returns ["Repeat"]</blockquote>
|
||||
|
||||
```js
|
||||
let testStr = "Repeat, Repeat, Repeat";
|
||||
let ourRegex = /Repeat/;
|
||||
testStr.match(ourRegex);
|
||||
// Returns ["Repeat"]
|
||||
```
|
||||
|
||||
To search or extract a pattern more than once, you can use the <code>g</code> flag.
|
||||
<blockquote>let repeatRegex = /Repeat/g;<br>testStr.match(repeatRegex);<br>// Returns ["Repeat", "Repeat", "Repeat"]</blockquote>
|
||||
|
||||
```js
|
||||
let repeatRegex = /Repeat/g;
|
||||
testStr.match(repeatRegex);
|
||||
// Returns ["Repeat", "Repeat", "Repeat"]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,9 +9,23 @@ challengeType: 1
|
||||
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:
|
||||
<blockquote>"z"<br>"zzzzzz"<br>"ABCzzzz"<br>"zzzzABC"<br>"abczzzzzzzzzzzzzzzzzzzzzabc"</blockquote>
|
||||
|
||||
```js
|
||||
"z"
|
||||
"zzzzzz"
|
||||
"ABCzzzz"
|
||||
"zzzzABC"
|
||||
"abczzzzzzzzzzzzzzzzzzzzzabc"
|
||||
```
|
||||
|
||||
But it does not find matches in the following strings since there are no letter <code>z</code> characters:
|
||||
<blockquote>""<br>"ABC"<br>"abcabc"</blockquote>
|
||||
|
||||
```js
|
||||
""
|
||||
"ABC"
|
||||
"abcabc"
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,7 +8,18 @@ challengeType: 1
|
||||
<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>).
|
||||
<blockquote>let longHand = /[A-Za-z0-9_]+/;<br>let shortHand = /\w+/;<br>let numbers = "42";<br>let varNames = "important_var";<br>longHand.test(numbers); // Returns true<br>shortHand.test(numbers); // Returns true<br>longHand.test(varNames); // Returns true<br>shortHand.test(varNames); // Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let longHand = /[A-Za-z0-9_]+/;
|
||||
let shortHand = /\w+/;
|
||||
let numbers = "42";
|
||||
let varNames = "important_var";
|
||||
longHand.test(numbers); // Returns true
|
||||
shortHand.test(numbers); // Returns true
|
||||
longHand.test(varNames); // Returns true
|
||||
shortHand.test(varNames); // Returns true
|
||||
```
|
||||
|
||||
These shortcut character classes are also known as <code>shorthand character classes</code>.
|
||||
</section>
|
||||
|
||||
|
@@ -8,7 +8,15 @@ challengeType: 1
|
||||
<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.
|
||||
<blockquote>let humStr = "I'll hum a song";<br>let hugStr = "Bear hug";<br>let huRegex = /hu./;<br>humStr.test(huRegex); // Returns true<br>hugStr.test(huRegex); // Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let humStr = "I'll hum a song";
|
||||
let hugStr = "Bear hug";
|
||||
let huRegex = /hu./;
|
||||
humStr.test(huRegex); // Returns true
|
||||
hugStr.test(huRegex); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,7 +8,17 @@ challengeType: 1
|
||||
<section id='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 <code>caret</code> character (<code>^</code>) inside a <code>character set</code> to create a <code>negated character set</code> in the form <code>[^thingsThatWillNotBeMatched]</code>. Outside of a <code>character set</code>, the <code>caret</code> is used to search for patterns at the beginning of strings.
|
||||
<blockquote>let firstString = "Ricky is first and can be found.";<br>let firstRegex = /^Ricky/;<br>firstRegex.test(firstString);<br>// Returns true<br>let notFirst = "You can't find Ricky now.";<br>firstRegex.test(notFirst);<br>// Returns false</blockquote>
|
||||
|
||||
```js
|
||||
let firstString = "Ricky is first and can be found.";
|
||||
let firstRegex = /^Ricky/;
|
||||
firstRegex.test(firstString);
|
||||
// Returns true
|
||||
let notFirst = "You can't find Ricky now.";
|
||||
firstRegex.test(notFirst);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,7 +8,17 @@ challengeType: 1
|
||||
<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 <code>asterisk</code> or <code>star</code>: <code>*</code>.
|
||||
<blockquote>let soccerWord = "gooooooooal!";<br>let gPhrase = "gut feeling";<br>let oPhrase = "over the moon";<br>let goRegex = /go*/;<br>soccerWord.match(goRegex); // Returns ["goooooooo"]<br>gPhrase.match(goRegex); // Returns ["g"]<br>oPhrase.match(goRegex); // Returns null</blockquote>
|
||||
|
||||
```js
|
||||
let soccerWord = "gooooooooal!";
|
||||
let gPhrase = "gut feeling";
|
||||
let oPhrase = "over the moon";
|
||||
let goRegex = /go*/;
|
||||
soccerWord.match(goRegex); // Returns ["goooooooo"]
|
||||
gPhrase.match(goRegex); // Returns ["g"]
|
||||
oPhrase.match(goRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,7 +8,18 @@ challengeType: 1
|
||||
<section id='description'>
|
||||
In the last challenge, you learned to use the <code>caret</code> 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 <code>dollar sign</code> character <code>$</code> at the end of the regex.
|
||||
<blockquote>let theEnding = "This is a never ending story";<br>let storyRegex = /story$/;<br>storyRegex.test(theEnding);<br>// Returns true<br>let noEnding = "Sometimes a story will have to end";<br>storyRegex.test(noEnding);<br>// Returns false<br></blockquote>
|
||||
|
||||
```js
|
||||
let theEnding = "This is a never ending story";
|
||||
let storyRegex = /story$/;
|
||||
storyRegex.test(theEnding);
|
||||
// Returns true
|
||||
let noEnding = "Sometimes a story will have to end";
|
||||
storyRegex.test(noEnding);
|
||||
// Returns false
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,7 +8,15 @@ challengeType: 1
|
||||
<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>.
|
||||
<blockquote>let shortHand = /\W/;<br>let numbers = "42%";<br>let sentence = "Coding!";<br>numbers.match(shortHand); // Returns ["%"]<br>sentence.match(shortHand); // Returns ["!"]<br></blockquote>
|
||||
|
||||
```js
|
||||
let shortHand = /\W/;
|
||||
let numbers = "42%";
|
||||
let sentence = "Coding!";
|
||||
numbers.match(shortHand); // Returns ["%"]
|
||||
sentence.match(shortHand); // Returns ["!"]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,7 +9,17 @@ challengeType: 1
|
||||
You saw how you can use <code>character sets</code> 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 <code>character set</code>, you can define a range of characters to match using a <code>hyphen</code> character: <code>-</code>.
|
||||
For example, to match lowercase letters <code>a</code> through <code>e</code> you would use <code>[a-e]</code>.
|
||||
<blockquote>let catStr = "cat";<br>let batStr = "bat";<br>let matStr = "mat";<br>let bgRegex = /[a-e]at/;<br>catStr.match(bgRegex); // Returns ["cat"]<br>batStr.match(bgRegex); // Returns ["bat"]<br>matStr.match(bgRegex); // Returns null</blockquote>
|
||||
|
||||
```js
|
||||
let catStr = "cat";
|
||||
let batStr = "bat";
|
||||
let matStr = "mat";
|
||||
let bgRegex = /[a-e]at/;
|
||||
catStr.match(bgRegex); // Returns ["cat"]
|
||||
batStr.match(bgRegex); // Returns ["bat"]
|
||||
matStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -7,9 +7,22 @@ challengeType: 1
|
||||
## 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>:
|
||||
<blockquote>let testStr = "Hello, my name is Kevin.";<br>let testRegex = /Kevin/;<br>testRegex.test(testStr);<br>// Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let testStr = "Hello, my name is Kevin.";
|
||||
let testRegex = /Kevin/;
|
||||
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>.
|
||||
<blockquote>let wrongRegex = /kevin/;<br>wrongRegex.test(testStr);<br>// Returns false</blockquote>
|
||||
|
||||
```js
|
||||
let wrongRegex = /kevin/;
|
||||
wrongRegex.test(testStr);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
A future challenge will show how to match those other forms as well.
|
||||
</section>
|
||||
|
||||
|
@@ -8,7 +8,13 @@ challengeType: 1
|
||||
<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>.
|
||||
<blockquote>let whiteSpace = "Whitespace. Whitespace everywhere!"<br>let nonSpaceRegex = /\S/g;<br>whiteSpace.match(nonSpaceRegex).length; // Returns 32</blockquote>
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
let nonSpaceRegex = /\S/g;
|
||||
whiteSpace.match(nonSpaceRegex).length; // Returns 32
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,7 +9,14 @@ challengeType: 1
|
||||
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>.
|
||||
Also, it is possible to combine a range of letters and numbers in a single character set.
|
||||
<blockquote>let jennyStr = "Jenny8675309";<br>let myRegex = /[a-z0-9]/ig;<br>// matches all letters and numbers in jennyStr<br>jennyStr.match(myRegex);</blockquote>
|
||||
|
||||
```js
|
||||
let jennyStr = "Jenny8675309";
|
||||
let myRegex = /[a-z0-9]/ig;
|
||||
// matches all letters and numbers in jennyStr
|
||||
jennyStr.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,7 +9,19 @@ challengeType: 1
|
||||
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 <code>character classes</code>. 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>.
|
||||
<blockquote>let bigStr = "big";<br>let bagStr = "bag";<br>let bugStr = "bug";<br>let bogStr = "bog";<br>let bgRegex = /b[aiu]g/;<br>bigStr.match(bgRegex); // Returns ["big"]<br>bagStr.match(bgRegex); // Returns ["bag"]<br>bugStr.match(bgRegex); // Returns ["bug"]<br>bogStr.match(bgRegex); // Returns null</blockquote>
|
||||
|
||||
```js
|
||||
let bigStr = "big";
|
||||
let bagStr = "bag";
|
||||
let bugStr = "bug";
|
||||
let bogStr = "bog";
|
||||
let bgRegex = /b[aiu]g/;
|
||||
bigStr.match(bgRegex); // Returns ["big"]
|
||||
bagStr.match(bgRegex); // Returns ["bag"]
|
||||
bugStr.match(bgRegex); // Returns ["bug"]
|
||||
bogStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,7 +8,14 @@ challengeType: 1
|
||||
<section id='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>.
|
||||
<blockquote>let whiteSpace = "Whitespace. Whitespace everywhere!"<br>let spaceRegex = /\s/g;<br>whiteSpace.match(spaceRegex);<br>// Returns [" ", " "]<br></blockquote>
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
let spaceRegex = /\s/g;
|
||||
whiteSpace.match(spaceRegex);
|
||||
// Returns [" ", " "]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -11,9 +11,24 @@ There are two kinds of <code>lookaheads</code>: <code>positive lookahead</code>
|
||||
A <code>positive lookahead</code> 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 <code>negative lookahead</code> 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.
|
||||
Lookaheads are a bit confusing but some examples will help.
|
||||
<blockquote>let quit = "qu";<br>let noquit = "qt";<br>let quRegex= /q(?=u)/;<br>let qRegex = /q(?!u)/;<br>quit.match(quRegex); // Returns ["q"]<br>noquit.match(qRegex); // Returns ["q"]</blockquote>
|
||||
|
||||
```js
|
||||
let quit = "qu";
|
||||
let noquit = "qt";
|
||||
let quRegex= /q(?=u)/;
|
||||
let qRegex = /q(?!u)/;
|
||||
quit.match(quRegex); // Returns ["q"]
|
||||
noquit.match(qRegex); // Returns ["q"]
|
||||
```
|
||||
|
||||
A more practical use of <code>lookaheads</code> is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
|
||||
<blockquote>let password = "abc123";<br>let checkPass = /(?=\w{3,6})(?=\D*\d)/;<br>checkPass.test(password); // Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let password = "abc123";
|
||||
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
|
||||
checkPass.test(password); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -10,7 +10,14 @@ Some patterns you search for will occur multiple times in a string. It is wastef
|
||||
You can search for repeat substrings using <code>capture groups</code>. 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.
|
||||
The example below matches any word that occurs twice separated by a space:
|
||||
<blockquote>let repeatStr = "regex regex";<br>let repeatRegex = /(\w+)\s\1/;<br>repeatRegex.test(repeatStr); // Returns true<br>repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]</blockquote>
|
||||
|
||||
```js
|
||||
let repeatStr = "regex regex";
|
||||
let repeatRegex = /(\w+)\s\1/;
|
||||
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>
|
||||
|
||||
|
@@ -9,7 +9,17 @@ challengeType: 1
|
||||
You can specify the lower and upper number of patterns with <code>quantity specifiers</code> 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>.
|
||||
<blockquote>let A4 = "haaaah";<br>let A3 = "haaah";<br>let A100 = "h" + "a".repeat(100) + "h";<br>let multipleHA = /ha{3}h/;<br>multipleHA.test(A4); // Returns false<br>multipleHA.test(A3); // Returns true<br>multipleHA.test(A100); // Returns false</blockquote>
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
let A3 = "haaah";
|
||||
let A100 = "h" + "a".repeat(100) + "h";
|
||||
let multipleHA = /ha{3}h/;
|
||||
multipleHA.test(A4); // Returns false
|
||||
multipleHA.test(A3); // Returns true
|
||||
multipleHA.test(A100); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,7 +9,17 @@ challengeType: 1
|
||||
You can specify the lower and upper number of patterns with <code>quantity specifiers</code> 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>.
|
||||
<blockquote>let A4 = "haaaah";<br>let A2 = "haah";<br>let A100 = "h" + "a".repeat(100) + "h";<br>let multipleA = /ha{3,}h/;<br>multipleA.test(A4); // Returns true<br>multipleA.test(A2); // Returns false<br>multipleA.test(A100); // Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
let A2 = "haah";
|
||||
let A100 = "h" + "a".repeat(100) + "h";
|
||||
let multipleA = /ha{3,}h/;
|
||||
multipleA.test(A4); // Returns true
|
||||
multipleA.test(A2); // Returns false
|
||||
multipleA.test(A100); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,7 +9,15 @@ challengeType: 1
|
||||
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 <code>quantity specifiers</code>. 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>.
|
||||
<blockquote>let A4 = "aaaah";<br>let A2 = "aah";<br>let multipleA = /a{3,5}h/;<br>multipleA.test(A4); // Returns true<br>multipleA.test(A2); // Returns false</blockquote>
|
||||
|
||||
```js
|
||||
let A4 = "aaaah";
|
||||
let A2 = "aah";
|
||||
let multipleA = /a{3,5}h/;
|
||||
multipleA.test(A4); // Returns true
|
||||
multipleA.test(A2); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -8,9 +8,21 @@ challengeType: 1
|
||||
<section id='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.
|
||||
<blockquote>let wrongText = "The sky is silver.";<br>let silverRegex = /silver/;<br>wrongText.replace(silverRegex, "blue");<br>// Returns "The sky is blue."</blockquote>
|
||||
|
||||
```js
|
||||
let wrongText = "The sky is silver.";
|
||||
let silverRegex = /silver/;
|
||||
wrongText.replace(silverRegex, "blue");
|
||||
// Returns "The sky is blue."
|
||||
```
|
||||
|
||||
You can also access capture groups in the replacement string with dollar signs (<code>$</code>).
|
||||
<blockquote>"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');<br>// Returns "Camp Code"</blockquote>
|
||||
|
||||
```js
|
||||
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
|
||||
// Returns "Camp Code"
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@@ -9,7 +9,14 @@ challengeType: 1
|
||||
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.
|
||||
<blockquote>let testStr = "freeCodeCamp";<br>let testRegex = /Code/;<br>testRegex.test(testStr);<br>// Returns true</blockquote>
|
||||
|
||||
```js
|
||||
let testStr = "freeCodeCamp";
|
||||
let testRegex = /Code/;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
Reference in New Issue
Block a user