-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter9.js
64 lines (51 loc) · 1.68 KB
/
chapter9.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
// Fill in the regular expressions
verify(/cat|car/,
["my car", "bad cats"],
["camper", "high art"]);
verify(/pop|prop/,
["pop culture", "mad props"],
["plop", "prrrop"]);
verify(/ferr(et|y|ari)/,
["ferret", "ferry", "ferrari"],
["ferrum", "transfer A"]);
verify(/ious\b/,
["how delicious", "spacious room"],
["ruinous", "consciousness"]);
verify(/\s(\.|,|:|;)/,
["bad punctuation .", "bad punctuation ,", "bad punctuation :", "bad punctuation ;"],
["escape the period", "bad punctuation !"]);
verify(/\w{7}/,
["hottentottententen"],
["no", "hotten totten tenten"]);
verify(/\b[^\We]+\b/i,
["red platypus", "wobbling nest"],
["earth bed", "learning ape", "BEET"]);
function verify(regexp, yes, no) {
// Ignore unfinished exercises
if (regexp.source == "...") return;
for (let str of yes) if (!regexp.test(str)) {
console.log(`Failure to match '${str}'`);
}
for (let str of no) if (regexp.test(str)) {
console.log(`Unexpected match for '${str}'`);
}
}
let text = "'I'm the cook,' he said, 'it's my job.'";
// Change this call.
console.log(text.replace(/(\W)'|'(\W)|^'/g, "$1\"$2"));
// → "I'm the cook," he said, "it's my job."
// Fill in this regular expression.
let number = /^[+\-]?(\d+(\.\d*)?|\.\d+)([eE][+\-]?\d+)?$/;
// Tests:
for (let str of ["1", "-1", "+15", "1.55", ".5", "5.",
"1.3e2", "1E-4", "1e+12"]) {
if (!number.test(str)) {
console.log(`Failed to match '${str}'`);
}
}
for (let str of ["1a", "+-1", "1.2.3", "1+1", "1e4.5",
".5.", "1f5", "."]) {
if (number.test(str)) {
console.log(`Incorrectly accepted '${str}'`);
}
}