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
2021-01-13 03:31:00 +01:00
dashedName: extract-matches
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --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 `.match()` method.
To use the `.match()` method, apply the method on a string and pass in the regex inside the parentheses.
2020-04-25 14:35:46 +01:00
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');
```
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 `.match()` method to extract the word `coding` .
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
The `result` should have the word `coding`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(result.join() === 'coding');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
Your regex `codingRegex` should search for `coding`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(codingRegex.source === 'coding');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should use the `.match()` method.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(code.match(/\.match\(.*\)/));
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
2020-11-27 19:02:05 +01:00
```js
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /change/; // Change this line
let result = extractStr; // Change this line
```
2018-09-30 23:01:58 +01:00
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 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
```