2018-09-30 23:01:58 +01:00
---
id: 587d7db4367417b2b2512b92
title: Extract Matches
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301340
2018-09-30 23:01:58 +01:00
---
## Description
< 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.
2020-04-25 14:35:46 +01:00
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:
2019-05-17 06:20:30 -07:00
```js
"Hello, World!".match(/Hello/);
// Returns ["Hello"]
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
// Returns ["expressions"]
```
2020-04-25 14:35:46 +01:00
Note that the `.match` syntax is the "opposite" of the `.test` method you have been using thus far:
```js
'string'.match(/regex/);
/regex/.test('string');
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
Apply the < code > .match()< / code > method to extract the word < code > coding< / code > .
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: The < code > result</ code > should have the word < code > coding</ code >
2019-07-24 02:32:04 -07:00
testString: assert(result.join() === "coding");
2018-10-04 14:37:37 +01:00
- text: Your regex < code > codingRegex</ code > should search for < code > coding</ code >
2019-07-24 02:32:04 -07:00
testString: assert(codingRegex.source === "coding");
2018-10-04 14:37:37 +01:00
- text: You should use the < code > .match()</ code > method.
2019-07-24 02:32:04 -07:00
testString: assert(code.match(/\.match\(.*\)/));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /change/; // Change this line
let result = extractStr; // Change this line
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-05-03 03:05:26 -07:00
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
< / section >