-
Notifications
You must be signed in to change notification settings - Fork 4
/
example-url-advanced.ts
239 lines (210 loc) · 7.91 KB
/
example-url-advanced.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import {
anyOf,
buildRegExp,
capture,
charClass,
charRange,
digit,
endOfString,
negativeLookahead,
oneOrMore,
optional,
repeat,
startOfString,
} from '../index';
// URL = Scheme ":"["//" Authority]Path["?" Query]["#" Fragment]
// Source: https://en.wikipedia.org/wiki/URL#External_links
// The building blocks of the URL regex.
const lowercase = charRange('a', 'z');
const uppercase = charRange('A', 'Z');
const hyphen = anyOf('-');
const alphabetical = charClass(lowercase, uppercase);
const specialChars = anyOf('._%+-');
const portSeperator = ':';
const schemeSeperator = ':';
const doubleSlash = '//';
const at = '@';
const pathSeparator = '/';
const querySeparator = '?';
const fragmentSeparator = '#';
const usernameChars = charClass(lowercase, digit, specialChars);
const hostnameChars = charClass(charRange('a', 'z'), digit, anyOf('-'));
const domainChars = charRange('a', 'z');
// Scheme:
// The scheme is the first part of the URL and defines the protocol to be used.
// Examples of popular schemes include http, https, ftp, mailto, file, data and irc.
// A URL string must be a scheme, followed by a colon, followed by a scheme-specific part.
const scheme = [repeat(charClass(hyphen, alphabetical), { min: 3, max: 6 }), optional('s')];
const schemeRegex = buildRegExp([startOfString, capture(scheme), endOfString], {
ignoreCase: true,
});
test('Matching the Schema components.', () => {
expect(schemeRegex).toMatchString('ftp');
expect(schemeRegex).not.toMatchString('ftp:');
expect(schemeRegex).not.toMatchString('h');
expect(schemeRegex).not.toMatchString('nameiswaytoolong');
expect(schemeRegex).toMatchString('HTTPS');
expect(schemeRegex).toMatchString('http');
});
// Authority:
// The authority part of a URL consists of three sub-parts:
// 1. An optional username, followed by an at symbol (@)
// 2. A hostname (e.g. www.google.com)
// 3. An optional port number, preceded by a colon (:)
// Authority = [userinfo "@"] host [":" port]
const userInfo = oneOrMore(usernameChars);
const hostname = repeat(hostnameChars, { min: 1, max: 63 });
const hostnameEnd = capture([hostname, endOfString]);
const host = capture([oneOrMore([hostname, '.'])]);
const port = [portSeperator, oneOrMore(digit)];
const authority = [doubleSlash, optional([userInfo, at]), hostname, optional(port)];
const authorityRegex = buildRegExp([startOfString, ...authority, endOfString], {
ignoreCase: true,
});
const hostEx = buildRegExp([startOfString, host, hostnameEnd, endOfString], { ignoreCase: true });
test('match URL hostname component', () => {
expect(hostEx).toMatchString('www.google.com');
expect(hostEx).not.toMatchString('www.google.com.');
});
test('match URL authority components', () => {
expect(authorityRegex).toMatchString('//davidbowie@localhost:8080');
expect(authorityRegex).toMatchString('//localhost:1234');
expect(authorityRegex).not.toMatchString('davidbowie@localhost:1972');
expect(authorityRegex).not.toMatchString('nameiswaytoolong');
});
// Path:
// The path is the part of the URL that comes after the authority and before the query.
// It consists of a sequence of path segments separated by a forward slash (/).
// A path string must begin with a forward slash (/).
const pathSegment = [
pathSeparator,
optional(oneOrMore(charClass(lowercase, uppercase, digit, anyOf(':@%._+~#=')))),
];
const path = oneOrMore(pathSegment);
const pathRegex = buildRegExp([startOfString, path, endOfString], {
ignoreCase: true,
});
test('match URL Path components.', () => {
expect(pathRegex).toMatchString('/');
expect(pathRegex).not.toMatchString('');
expect(pathRegex).toMatchString('/a');
expect(pathRegex).not.toMatchString('a');
expect(pathRegex).not.toMatchString('a/');
expect(pathRegex).toMatchString('/a/b');
expect(pathRegex).not.toMatchString('a/b');
expect(pathRegex).not.toMatchString('a/b/');
});
// Query:
// The query part of a URL is optional and comes after the path.
// It is separated from the path by a question mark (?).
// The query string consists of a sequence of field-value pairs separated by an ampersand (&).
// Each field-value pair is separated by an equals sign (=).
const queryKey = oneOrMore(charClass(lowercase, uppercase, digit, anyOf('_-')));
const queryValue = oneOrMore(charClass(lowercase, uppercase, digit, anyOf('_-')));
const queryDelimiter = anyOf('&;');
const equals = '=';
const queryKeyValuePair = buildRegExp([queryKey, equals, queryValue]);
const query = [querySeparator, oneOrMore([queryKeyValuePair, optional(queryDelimiter)])];
const queryRegex = buildRegExp([startOfString, ...query, endOfString], {
ignoreCase: true,
});
test('match URL query components', () => {
expect(queryRegex).not.toMatchString('');
expect(queryRegex).not.toMatchString('??');
expect(queryRegex).not.toMatchString('?');
expect(queryRegex).not.toMatchString('?a-b');
expect(queryRegex).toMatchString('?a=b');
expect(queryRegex).toMatchString('?a=b&c=d');
expect(queryRegex).not.toMatchString('a=b&c-d');
});
// Fragment:
// The fragment part of a URL is optional and comes after the query.
// It is separated from the query by a hash (#).
// The fragment string consists of a sequence of characters.
const fragment = [
fragmentSeparator,
oneOrMore(charClass(lowercase, uppercase, digit, anyOf(':@%._+~#=&'))),
];
const fragmentRegex = buildRegExp([startOfString, ...fragment, endOfString], {
ignoreCase: true,
});
test('match URL fragment component', () => {
expect(fragmentRegex).not.toMatchString('');
expect(fragmentRegex).toMatchString('#section1');
expect(fragmentRegex).not.toMatchString('#');
});
const urlRegex = buildRegExp(
[
startOfString,
capture([
optional(scheme),
schemeSeperator,
optional(authority),
path,
optional(query),
optional(fragment),
]),
endOfString,
],
{
ignoreCase: true,
},
);
test('match URLs', () => {
expect(urlRegex).not.toMatchString('');
expect(urlRegex).not.toMatchString('http');
expect(urlRegex).toMatchString('http://localhost:8080');
expect(urlRegex).toMatchString('http://localhost:8080/users/paul/research/data.json');
expect(urlRegex).toMatchString(
'http://localhost:8080/users/paul/research/data.json?request=regex&email=me',
);
expect(urlRegex).toMatchString(
'http://localhost:8080/users/paul/research/data.json?request=regex&email=me#section1',
);
});
const emailRegex = buildRegExp(
[
startOfString,
capture([
oneOrMore(usernameChars),
'@',
oneOrMore(hostnameChars),
'.',
repeat(domainChars, { min: 2 }),
]),
endOfString,
],
{
ignoreCase: true,
},
);
test('match email address', () => {
expect(emailRegex).not.toMatchString('');
expect(emailRegex).toMatchString('stevenwilson@porcupinetree.com');
expect(emailRegex).not.toMatchString('stevenwilson@porcupinetree');
});
const urlsWithoutEmailsRegex = buildRegExp(
[
startOfString,
negativeLookahead(emailRegex), // Reject emails
urlRegex,
endOfString,
],
{
ignoreCase: true,
},
);
test('match URL but not email', () => {
expect(urlsWithoutEmailsRegex).toMatchString('http://localhost:8080');
expect(urlsWithoutEmailsRegex).toMatchString(
'http://paul@localhost:8080/users/paul/research/data.json?request=regex&email=me#section1',
);
expect(urlsWithoutEmailsRegex).toMatchString('ftp://data/#January');
expect(urlsWithoutEmailsRegex).not.toMatchString('https:');
expect(urlsWithoutEmailsRegex).not.toMatchString('piotr@riverside.com');
expect(urlsWithoutEmailsRegex).toMatchString('http://www.google.com');
expect(urlsWithoutEmailsRegex).toMatchString('https://www.google.com?search=regex');
expect(urlsWithoutEmailsRegex).not.toMatchString('www.google.com?search=regex&email=me');
expect(urlsWithoutEmailsRegex).toMatchString('mailto://paul@thebeatles.com');
expect(urlsWithoutEmailsRegex).not.toMatchString('ftphttpmailto://neal@nealmorse');
});