2018-09-30 23:01:58 +01:00
---
id: 587d7db3367417b2b2512b8e
title: Using the Test Method
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301369
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching.
2020-11-27 19:02:05 +01: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.
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.
2019-05-17 06:20:30 -07:00
```js
let testStr = "freeCodeCamp";
let testRegex = /Code/;
testRegex.test(testStr);
// Returns true
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should use `.test()` to test the regex.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
Your result should return `true` .
```js
assert(result === true);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex; // Change this line
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-05-03 03:05:26 -07:00
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // Change this line
2018-09-30 23:01:58 +01:00
```