Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
check-for-all-or-none.english.md
extract-matches.english.md
find-characters-with-lazy-matching.english.md
find-more-than-the-first-match.english.md
find-one-or-more-criminals-in-a-hunt.english.md
ignore-case-while-matching.english.md
match-a-literal-string-with-different-possibilities.english.md
match-all-letters-and-numbers.english.md
match-all-non-numbers.english.md
match-all-numbers.english.md
match-anything-with-wildcard-period.english.md
match-beginning-string-patterns.english.md
match-characters-that-occur-one-or-more-times.english.md
match-characters-that-occur-zero-or-more-times.english.md
match-ending-string-patterns.english.md
match-everything-but-letters-and-numbers.english.md
match-letters-of-the-alphabet.english.md
match-literal-strings.english.md
match-non-whitespace-characters.english.md
match-numbers-and-letters-of-the-alphabet.english.md
match-single-character-with-multiple-possibilities.english.md
match-single-characters-not-specified.english.md
match-whitespace.english.md
positive-and-negative-lookahead.english.md
remove-whitespace-from-start-and-end.english.md
restrict-possible-usernames.english.md
reuse-patterns-using-capture-groups.english.md
specify-exact-number-of-matches.english.md
specify-only-the-lower-number-of-matches.english.md
specify-upper-and-lower-number-of-matches.english.md
use-capture-groups-to-search-and-replace.english.md
using-the-test-method.english.md
03-front-end-libraries
04-data-visualization
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
adddir.js
translatemd.js
formattingConversion
math-challenges
requiresTests
schema
.babelrc
.editorconfig
.eslintignore
.eslintrc
.npmignore
.prettierrc
.travis.yml
CHANGELOG.md
LICENSE.md
addAssertsToTapTest.js
challengeTitles.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
mongoIds.js
package-entry.js
package-lock.json
package.json
repack.js
test-challenges.js
unpack.js
unpacked.css
unpacked.js
unpackedChallenge.js
utils.js
docs
news
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env

58 lines
1.8 KiB
Markdown
Raw Normal View History

---
id: 587d7db3367417b2b2512b8e
title: Using the Test Method
challengeType: 1
---
## Description
<section id='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.
<blockquote>let testStr = "freeCodeCamp";<br>let testRegex = /Code/;<br>testRegex.test(testStr);<br>// Returns true</blockquote>
</section>
## Instructions
<section id='instructions'>
Apply the regex <code>myRegex</code> on the string <code>myString</code> using the <code>.test()</code> method.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: You should use <code>.test()</code> to test the regex.
testString: 'assert(code.match(/myRegex.test\(\s*myString\s*\)/), "You should use <code>.test()</code> to test the regex.");'
- text: Your result should return <code>true</code>.
testString: 'assert(result === true, "Your result should return <code>true</code>.");'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex; // Change this line
```
</div>
</section>
## Solution
<section id='solution'>
```js
// solution required
```
</section>