-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJavaScript Regular Expressions
147 lines (131 loc) · 5.03 KB
/
JavaScript Regular Expressions
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
JavaScript Regular Expressions
Chapter 1: Getting Started w/ Regex
Regular Expression represents paterns syntactically with mechanical syntax
RegExp is available in nearly all languages
It's all about patterns
Analyzing a phone number
Identify mandatory and optional parts and their constraints from examples
Ex. phone number
mandatory: 10 digits (constraint: 0-9)
optional: dashes, brackets and spaces
Analyzing a simple log file
improve accuracy by targeting specific keyword
Analyzing an XML file
keep track of repeating element like opening and closing tag
Regex in JavaScript
as RegExp object that can be used to test and manipulate strings
Ex. regular expression that matches "hello"
var rgx1 = new RegExp("hello");
var rgx2 = /hello/;
The RegExp constructor takes string - special characters need to be escaped
Using pattern flags as second parameter that applies on entire expression
"i" flag: ignore case - standard pattern are case sensitive, i flag enable selection of either case
"m" flag: multiline - treat each line in the string as a new string to be matched
"g" flag: global
- return all matches
- RegExp only return first found match without g flag
Ex.
var rgx1 = new RegExp("hello", "gi");
var rgx2 = /hello/gi;
Using the rgx.test method
return true or false based on if there is a match
Ex.
var rgx = /hello/;
console.log(rgx.test("hello")); // true
Using the rgx.exec method
Ex.
var rgx = /world/g;
var result = rgx.exec("world !!"); // return null if there is NO match
console.log(result.length); // 1
console.log(result[0]); // world
console.log(result[1]); // undefined - no () in rgx
console.log(result.index); // starting index: 0
console.log(result.input); // original input text: world !!
console.log(rgx.lastIndex); // integer property lastIndex (index where next search start): 0 without g flag, 5 with g flag
console.log(rgx.ignoreCase); // false
console.log(rgx.global); // true
console.log(rgx.multiline); // false
console.log(rgx.source); // world
The string object and regular expressions
Using the String.replace method only replace first occurrence
Ex.
str = "foo foo";
console.log(str.replace("foo", "qux")); // foo qux
Supply String.replace with RegExp and g flag, /foo/g, to replace ALL occurrences
Ex.
str = "foo foo";
console.log(str.replace(/foo/g, "qux")); // qux qux
Using the String.search method
Using the String.match method
Building our environment
Handling a submitted form
Resetting matches and errors
Creating a regular expression
Executing RegExp and extracting its matches
Testing our application
Summary
Chapter 2: The Basics
Defining vague matchers in Regex
Matching a wild card character
Matching digits
Matching alphanumeric chars
Negating alphanumeric chars and digits
Defining ranges in Regex
Defining a ranges
Matching the dash character
Defining negated ranges
Defining multipliers in Regex
Matching one or more occurrences
Matching zero or one occurrence
Matching zero or more occurrences
Defining custom quantifiers
Matching n or more occurrences
Matching n to m occurrences
Matching alternated options
Creating a Regex for a telephone number
Summary
Chapter 3: Special Characters
Nonvisual constraints
Matching the begining and end of an input
Matching word boundaries
Matching nonword boundaries
Matching a whitespace character
Define nongreedy quantifiers
Matching groups in Regex
Grouping characters together to create a clause
Capture and noncapture groups
Matching lookahead groups
Using a negative lookahead
Summary
Chapter 4: Regex in Practice
Regular expressions and form validation
Setting up the form
Validating fields
Matching a complete name
Understanding the complete name Regex
Matching an e-mail with Regex
Understanding the email Regex
Matching a Twitter name
Understanding the twitter username Regex
Matching passwords
Matching URLs
Understanding the URL Regex
Manipulating data
Using the String.replace method
Matching a description field
Understanding the description Regex
Explaining a Markdown example
Summary
Chapter 5: Node.js and Regex
Setting up Node.js
Getting started with our application
Reading a file with Node.js
The anatomy of an Apache log file
Creating the Apache log Regex
Creating a Regex for the time part
Creating a Regex for the request information
Creating a Regex for the status code and object size
Creating a Regex for the referrer and the user agent
Parsing each Apache log now
Summary
Appendix A. JavaScript Regex Cheat Sheet