* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
59 lines
1.4 KiB
Markdown
59 lines
1.4 KiB
Markdown
---
|
|
id: 587d7db3367417b2b2512b8e
|
|
title: Using the Test Method
|
|
challengeType: 1
|
|
forumTopicId: 301369
|
|
dashedName: using-the-test-method
|
|
---
|
|
|
|
# --description--
|
|
|
|
Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching.
|
|
|
|
If you want to find the word `"the"` in the string `"The dog chased the cat"`, you could use the following regular expression: `/the/`. Notice that quote marks are not required within the regular expression.
|
|
|
|
JavaScript has multiple ways to use regexes. One way to test a regex is using the `.test()` method. The `.test()` method takes the regex, applies it to a string (which is placed inside the parentheses), and returns `true` or `false` if your pattern finds something or not.
|
|
|
|
```js
|
|
let testStr = "freeCodeCamp";
|
|
let testRegex = /Code/;
|
|
testRegex.test(testStr);
|
|
// Returns true
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
|
|
|
|
# --hints--
|
|
|
|
You should use `.test()` to test the regex.
|
|
|
|
```js
|
|
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
|
```
|
|
|
|
Your result should return `true`.
|
|
|
|
```js
|
|
assert(result === true);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
let myString = "Hello, World!";
|
|
let myRegex = /Hello/;
|
|
let result = myRegex; // Change this line
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
let myString = "Hello, World!";
|
|
let myRegex = /Hello/;
|
|
let result = myRegex.test(myString); // Change this line
|
|
```
|