2018-10-10 18:03:03 -04:00
---
id: 587d7db4367417b2b2512b92
2021-02-06 04:42:36 +00:00
title: Extract Matches
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:14:01 +08:00
forumTopicId: 301340
2021-01-13 03:31:00 +01:00
dashedName: extract-matches
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
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.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
To use the `.match()` method, apply the method on a string and pass in the regex inside the parentheses.
Here's an example:
2020-08-04 15:14:01 +08:00
```js
"Hello, World!".match(/Hello/);
// Returns ["Hello"]
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
// Returns ["expressions"]
```
2021-02-06 04:42:36 +00: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-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 `.match()` method to extract the word `coding` .
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
The `result` should have the word `coding`
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(result.join() === 'coding');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your regex `codingRegex` should search for `coding`
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(codingRegex.source === 'coding');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should use the `.match()` method.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/\.match\(.*\)/));
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 extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /change/; // Change this line
let result = extractStr; // Change this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line
```