-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter8-Regex.html
149 lines (115 loc) · 3.65 KB
/
chapter8-Regex.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<html>
<body>
<script type="text/javascript">
"doubledare".search(/le/);
var slash=/\//;
"AC/DC".search(slash);
//Matching Sets of Characters
var asteriskOrBrace=/[\{\*]/;
var story = "We noticed the *giant sloth*, hanging from a giant branch";
story.search(asteriskOrBrace);
var digitSurroundedBySpace = /\s\d\s/;
var notABC= /[^ABC]/;
"ABCBACCBADABC".search(notABC);
var datePattern=/\d\d\/\d\d\/\d\d\d\d/;
//Matching Word and String Boundaries
/a/.test("blah");
/^a$/.test("blah");
/cat/.test("concatenate");
// true
/\bcat\b/.test("concatenate");
// false
//Repeating Patterns
var parentheticText = /\(.*\)/;
"Its (the sloth's) claws were gigantic!".search(parentheticText);
// 4
var datePattern = /\d{1,2}\/\d\d?\/\d{4}/;
"born 15/11/2003 (mother Spot): White Fang".search(datePattern);
// 5
//Grouping Subexpressions
var cartoonCrying = /boo(hoo+)+/i;
cartoonCrying.test("Boohoooohoohooo");
// true
//Choosing Between Alternatives
var holyCow = /\b(sacred|holy) (cow|bovine|bull|taures)\b/i;
holyCow.test("Sacred bovine!");
//true
//Matching and Replacing
//The "match" method
"No".match(/yes/i);
// null
"...yes".match(/yes/i);
// ["yes"]
"Giant Ape".match(/giant (w\+)/i);
// ["Giant Ape", "Ape"]
function extractDate(string) {
var found = string.match(/\b(\d\d?)\/(\d\d?)\/(\d{4})\b/);
if (found == null) {
throw new Error("No date found in '" + string + "'.");
}
return new Date(Number(found[3]), Number(found[2] -1, Number(found[1]));
}
//The "replace" method
"Borodubur".replace(/[ou]/g, "a");
// Barabadar
var names = "Picasso, Pablo\nGauguin, Paul\nVan Gogh, Vincent";
names.replace(/([\w ]+), ([\w ]+)/g, "$2 $1");
// "Pablo Picasso\nPaul Gauguin\nVincent Van Gogh"
//When a function is the second parameter instead of a string, the function gets called whenever a match is found in the original calling string
"the cia and fbi".replace(/\b(fbi|cia)\b/g), function(str) {
return str.toUpperCase();
});
// "the CIA and FBI"
//From Chapter 5
function escapeHTML(text) {
var replacements = [["&", "&"],["\"", """]];
forEach(replacements, function(replace) {
text = text.replace(replace[0], replace[1]);
});
return text;
}
//Newer version only calls replace once
function escapeHTML(text) {
var replacements = {"&": "&", "\"": """};
return text.replace(/[<>&"]/g, function(character) {
return replacements[character];
});
}
//Dynamically Create Regex Objects
var badWords = ["ape", "monkey"];
var pattern = new RegExp(badWords.join("|"), "i");
function isAcceptable(text){
return !pattern.test(text);
}
isAcceptable("The quick brown fox");
//True
isAcceptable("Cut that monkeybusiness");
//false
//Backslashes you want in the regex must be escaped
var digits = new RegExp("\\d+");
//Parsing .ini files
function parseINI(string) {
var lines=splitLines(string);
var categories=[];
function newCategory(name) {
var cat={name: name, fields: []};
categories.push(cat);
return cat;
}
var currentCategory=newCategory("TOP");
forEach(lines, function(line) {
var match;
if(/^\s*(;.*)?$/.test(line))
return;
else if (match = line.match(/^\[(.*)\]$/))
currentCategory=newCategory(match[1]);
else if (match = line.match(/^(\w+)=(.*)$/))
currentCategory.fields.push({name: match[1], value: match[2]});
else
throw new Error("Line '" + line + "' is invalid.");
});
return categories;
}
</script>
</body>
</html>