-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregular_expressions_exercise1.js
51 lines (38 loc) · 1.17 KB
/
regular_expressions_exercise1.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
// 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(/\w+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/g,
["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 + "'");
// Your code here.
});
var text = "'I'm the cook,' he said, 'it's my job.'";
// Change this call.
console.log(text.replace(/'([^m])|\W'|'([^t'])\w/g, "\"$1"));
// → "I'm the cook," he said, "it's my job."