-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstants.ts
47 lines (43 loc) · 1.3 KB
/
constants.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
const ipv4Part = '(0?\\d+|0x[a-f0-9]+)'
const ipv4Regexes = {
fourOctet: new RegExp(
`^${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}$`,
'i'
),
threeOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}$`, 'i'),
twoOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}$`, 'i'),
longValue: new RegExp(`^${ipv4Part}$`, 'i'),
}
// Regular Expression for checking Octal numbers
const octalRegex = new RegExp(`^0[0-7]+$`, 'i')
const hexRegex = new RegExp(`^0x[a-f0-9]+$`, 'i')
const zoneIndex = '%[0-9a-z]{1,}'
// IPv6-matching regular expressions.
// For IPv6, the task is simpler: it is enough to match the colon-delimited
// hexadecimal IPv6 and a transitional variant with dotted-decimal IPv4 at
// the end.
const ipv6Part = '(?:[0-9a-f]+::?)+'
const ipv6Regexes = {
zoneIndex: new RegExp(zoneIndex, 'i'),
native: new RegExp(
`^(::)?(${ipv6Part})?([0-9a-f]+)?(::)?(${zoneIndex})?$`,
'i'
),
deprecatedTransitional: new RegExp(
`^(?:::)(${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}(${zoneIndex})?)$`,
'i'
),
transitional: new RegExp(
`^((?:${ipv6Part})|(?:::)(?:${ipv6Part})?)${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}(${zoneIndex})?$`,
'i'
),
}
export {
ipv4Part,
ipv4Regexes,
ipv6Part,
ipv6Regexes,
zoneIndex,
octalRegex,
hexRegex,
}