-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREGEX_Match_a_Literal_String_with_Different_Possibilities.js
More file actions
39 lines (26 loc) · 1.31 KB
/
REGEX_Match_a_Literal_String_with_Different_Possibilities.js
File metadata and controls
39 lines (26 loc) · 1.31 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
/*
Match a Literal String with Different Possibilities
Using regexes like /coding/, you can look for the pattern coding in
another string.
This is powerful to search single strings, but it's limited to only
one pattern. You can search for multiple patterns using the alternation
or OR operator: |.
This operator matches patterns either before or after it.
For example, if you wanted to match the strings yes or no,
the regex you want is /yes|no/.
You can also search for more than just two patterns.
You can do this by adding more patterns with more OR operators
separating them, like /yes|no|maybe/.
EXERCISE
Complete the regex petRegex to match the pets dog, cat, bird, or fish.
Your regex petRegex should return true for the string John has a pet dog.
Your regex petRegex should return false for the string Emma has a pet rock.
Your regex petRegex should return true for the string Emma has a pet bird.
Your regex petRegex should return true for the string Liz has a pet cat.
Your regex petRegex should return false for the string Kara has a pet dolphin.
Your regex petRegex should return true for the string Alice has a pet fish.
Your regex petRegex should return false for the string Jimmy has a pet computer.
*/
let petString = "James has a pet cat.";
let petRegex = /dog|bird|cat|fish/;
let result = petRegex.test(petString);