-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_numbers.js
34 lines (24 loc) · 1.03 KB
/
regex_numbers.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
/*
FIND NUMBERS WITH REGULAR EXPRESSIONS
We can use special selectors in Regular Expressions to select a particular type of
value.
One such selector is the digit selector \d which is used to retrieve one digit
(e.g. numbers 0 to 9) in a string.
In JavaScript, it is used like this: /\d/g.
Appending a plus sign (+) after the selector, e.g. /\d+/g, allows this regular
expression to match one or more digits.
The trailing g is short for 'global', which allows this regular expression to find
all matches rather than stop at the first match.
*/
/* INSTRUCTIONS
Use the \d selector to select the number of numbers in the string, allowing for
the possibility of one or more digit.
*/
// Setup
var testString = "There are 3 cats but 4 dogs. Living in 10 houses";
// Only change code below this line.
var expression = /\d+/g; // Change this line
// Only change code above this line
// This code counts the matches of expression in testString
var digitCount = testString.match(expression).length;
console.log("Regex find number: ", digitCount);