-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 9.js
93 lines (69 loc) · 2.8 KB
/
Chapter 9.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Q-1: For each of the following items, write a regular expression
car and cat
pop and prop
ferret, ferry, and ferrari
Any word ending in ious
A whitespace character followed by a dot, comma, colon, or semicolon
A word longer than six letters
A word without the letter e*/
//Solution
// Fill in the regular expressions
verify(/ca[rt]/,
["my car", "bad cats"],
["camper", "high art"]);
verify(/pr?op/,
["pop culture", "mad props"],
["plop"]);
verify(/ferr(et|y|ari)/,
["ferret", "ferry", "ferrari"],
["ferrum", "transfer A"]);
verify(/ious\b/,
["how delicious", "spacious room"],
["ruinous", "consciousness"]);
verify(/\s[\.,:;]/,
["bad punctuation ."],
["escape the dot"]);
verify(/\w{7,}/,
["hottentottententen"],
["no", "hotten totten tenten"]);
verify(/\b[^e\s]\b/,
["red platypus", "wobbling nest"],
["earth bed", "learning ape"]);
function verify(regexp, yes, no) {
// Ignore unfinished exercises
if (regexp.source == "...") return;
yes.forEach(function(s) {
if (!regexp.test(s))
console.log("Failure to match '" + s + "'");
});
no.forEach(function(s) {
if (regexp.test(s))
console.log("Unexpected match for '" + s + "'");
});
}
/*Q-2: Imagine you have written a story and used single quotation marks throughout to mark pieces of dialogue. Now you want to replace all the dialogue quotes with double quotes,
while keeping the single quotes used in contractions like aren’t.
Think of a pattern that distinguishes these two kinds of quote usage and craft a call to the replace method that does the proper replacement.
*/
//Solution:
var text = "'I'm the cook,' he said, 'it's my job.'";
// Change this call.
console.log(text.replace(/(^)'|(\W)'|'(\W)|'($)/g, "$1$2\"$3"));
// → "I'm the cook," he said, "it's my job."
//Q-3: A series of digits can be matched by the simple regular expression /\d+/. Write an expression that matches only JavaScript-style numbers.
//It must support an optional minus or plus sign in front of the number, the decimal dot, and exponent notation—5e-3 or 1E10— again with an optional sign in front of the exponent.
//Also note that it is not necessary for there to be digits in front of or after the dot, but the number cannot be a dot alone. That is, .5 and 5. are valid JavaScript numbers, but a lone dot isn’t.
//Solution
// Fill in this regular expression.
var number = /^(\+|-)?((\d+(\.\d*)?)|(\.\d+))(((e|E)(\+|-)?)\d+)?$/;
// Tests:
["1", "-1", "+15", "1.55", ".5", "5.", "1.3e2", "1E-4",
"1e+12"].forEach(function(s) {
if (!number.test(s))
console.log("Failed to match '" + s + "'");
});
["1a", "+-1", "1.2.3", "1+1", "1e4.5", ".5.", "1f5",
"."].forEach(function(s) {
if (number.test(s))
console.log("Incorrectly accepted '" + s + "'");
});