Skip to content

Commit 381e40d

Browse files
authored
Merge pull request #72 from zanerock/work-liquid-labs/regex-repo/71
Add 'localhost' RE
2 parents e992007 + c9b1a07 commit 381e40d

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const allColors = cssContent
115115
- <span id="ipformatre">__`ipFormatRE`__</span>: Matches a string in IP address format. Use 'ipRE' to match actually valid IP addresses.
116116
- <span id="ipre">__`ipRE`__</span>: Matches a valid, non-localhost IP address.
117117
- <span id="ipv6re">__`ipV6RE`__</span>: Matches a string in IPV6 format.
118+
- <span id="localhostre">__`localhostRE`__</span>: Matches any representation of localhost; the special name, IPV4 loopbacks, or IPV6 loopbacks.
118119
- <span id="subdomainlabelre">__`subdomainLabelRE`__</span>: Matches a registerable domain name. Partially enforces the 63 byte domain label limit, but this is only valid for non-international (all ASCII) labels because we can only count characters. See [domain name rules](#domain-name-rules). When using the partial string to create a RE, you must use the 'u' or 'v' flag.
119120
- <span id="tldnamere">__`tldNameRE`__</span>: Matches a Top Level Domain (TLD). See [domain name rules](#domain-name-rules). When using the partial string to create a RE, you must use the 'u' or 'v' flag.
120121
- <span id="urlre">__`urlRE`__</span>: Matches a valid URL. When using the partial string to create a RE, you must use the 'u' or 'v' flag.

src/test/web.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ groupTestPartial(regex.ipFormatREString, goodIPFormats, badIPFormats, 'IP format
3939
groupTest(regex.ipV6RE, goodIPV6s, badIPV6s, 'IPV6')
4040
groupTestPartial(regex.ipV6REString, goodIPV6s, badIPV6s, 'IPV6')
4141

42+
const goodLocalhosts = ['127.0.0.1', '127.34.0.255', 'localhost', '::1', '0:0:0:0:0:0:0:1']
43+
const badLocalhosts = ['128.0.0.1', 'localhosts', '::2']
44+
45+
groupTest(regex.localhostRE, goodLocalhosts, badLocalhosts, 'localhosts')
46+
groupTestPartial(regex.localhostREString, goodLocalhosts, badLocalhosts, 'localhosts')
47+
4248
groupTest(regex.tldNameRE, goodTLDs, badTLDs, 'TLDs')
4349
groupTestPartial(regex.tldNameREString, goodTLDs, badTLDs, 'TLDs', undefined, undefined, 'u')
4450

src/web.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export const ipV6REString =
5252
// Web: Matches a string in IPV6 format.
5353
export const ipV6RE = lockdownRE(ipV6REString)
5454

55+
export const localhostREString = `(?:localhost|127(?:\\.${ipTuple}){3}|::1|0:0:0:0:0:0:0:1)`
56+
// Web: Matches any representation of localhost; the special name, IPV4 loopbacks, or IPV6 loopbacks.
57+
export const localhostRE = lockdownRE(localhostREString)
58+
5559
// note the 'v' flag breaks on Ubuntu
5660
export const tldNameREString = `(?:[${uniNonASCII}]|[a-zA-Z${uniNonASCII}][a-zA-Z0-9${uniNonASCII}]{1,62})`
5761
// Web: Matches a Top Level Domain (TLD). See [domain name rules](#domain-name-rules). When using the partial string to create a RE, you must use the 'u' or 'v' flag.

0 commit comments

Comments
 (0)