Differentiate between test & match (#38498)
* Differentiate between test & match I noticed that nowhere was there a mention that .match() and .test() pass in and are applied to opposite objects. This would've saved me a few minutes of searching during later challenges that assume this is understood. * Add in .match & .test difference after example * fix: add spaces to stop lint errors Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
@ -8,7 +8,8 @@ forumTopicId: 301340
|
|||||||
## Description
|
## Description
|
||||||
<section id='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.
|
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.
|
||||||
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:
|
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:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
"Hello, World!".match(/Hello/);
|
"Hello, World!".match(/Hello/);
|
||||||
@ -19,6 +20,13 @@ ourStr.match(ourRegex);
|
|||||||
// Returns ["expressions"]
|
// 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');
|
||||||
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
Reference in New Issue
Block a user