-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_string.js
48 lines (32 loc) · 1.37 KB
/
regex_string.js
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
42
43
44
45
46
47
48
/*
SIFT THROUGH TEXT WITH REGULAR EXPRESSIONS
Regular expressions are used to find certain words or patterns inside of strings.
For example, if we wanted to find the word the in the string The dog chased the cat,
we could use the following regular expression: /the/gi
Let's break this down a bit:
/ is the start of the regular expression.
the is the pattern we want to match.
/ is the end of the regular expression.
g means global, which causes the pattern to return all matches in the string, not
just the first one.
i means that we want to ignore the case (uppercase or lowercase) when searching for
the pattern.
*/
/*
INSTRUCTIONS
Select all the occurrences of the word and in testString.
You can do this by replacing the . part of the regular expression with the word
and.
*/
// Setup
var testString = "Ada Lovelace and Charles Babbage designed the first computer and the software that would have run on it.";
// Example
var expressionToGetSoftware = /software/gi;
var softwareCount = testString.match(expressionToGetSoftware).length;
// Only change code below this line.
var expression = /and/gi; // Change this Line
// Only change code above this line
// This code counts the matches of expression in testString
var andCount = testString.match(expression).length;
console.log("What\'s in expression var: ", expression);
console.log("What\'s in andCount var: ", andCount);