* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
74 lines
1.5 KiB
Markdown
74 lines
1.5 KiB
Markdown
---
|
|
id: 587d7db4367417b2b2512b92
|
|
title: Extract Matches
|
|
challengeType: 1
|
|
forumTopicId: 301340
|
|
dashedName: extract-matches
|
|
---
|
|
|
|
# --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.
|
|
|
|
Here's an example:
|
|
|
|
```js
|
|
"Hello, World!".match(/Hello/);
|
|
// Returns ["Hello"]
|
|
let ourStr = "Regular expressions";
|
|
let ourRegex = /expressions/;
|
|
ourStr.match(ourRegex);
|
|
// Returns ["expressions"]
|
|
```
|
|
|
|
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');
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Apply the `.match()` method to extract the word `coding`.
|
|
|
|
# --hints--
|
|
|
|
The `result` should have the word `coding`
|
|
|
|
```js
|
|
assert(result.join() === 'coding');
|
|
```
|
|
|
|
Your regex `codingRegex` should search for `coding`
|
|
|
|
```js
|
|
assert(codingRegex.source === 'coding');
|
|
```
|
|
|
|
You should use the `.match()` method.
|
|
|
|
```js
|
|
assert(code.match(/\.match\(.*\)/));
|
|
```
|
|
|
|
# --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
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
let extractStr = "Extract the word 'coding' from this string.";
|
|
let codingRegex = /coding/; // Change this line
|
|
let result = extractStr.match(codingRegex); // Change this line
|
|
```
|