-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREGEX_Extract_Matches.js
More file actions
41 lines (29 loc) · 1.02 KB
/
REGEX_Extract_Matches.js
File metadata and controls
41 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Extract Matches
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:
"Hello, World!".match(/Hello/);
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
Here the first match would return ["Hello"]
and the second would return ["expressions"].
Note that the .match syntax is the "opposite"
of the .test method you have been using thus far:
'string'.match(/regex/);
/regex/.test('string');
EXERCISE
Apply the .match() method to extract the string coding.
The result should have the string coding
Your regex codingRegex should search for the string coding
You should use the .match() method.
*/
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line