2018-10-10 18:03:03 -04:00
---
id: 587d7db3367417b2b2512b8e
2021-02-06 04:42:36 +00:00
title: Using the Test Method
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:14:01 +08:00
forumTopicId: 301369
2021-01-13 03:31:00 +01:00
dashedName: using-the-test-method
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
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.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
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.
2020-08-04 15:14:01 +08:00
```js
let testStr = "freeCodeCamp";
let testRegex = /Code/;
testRegex.test(testStr);
// Returns true
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should use `.test()` to test the regex.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your result should return `true` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(result === true);
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:14:01 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex; // Change this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // Change this line
```