@common.js/ip-regex 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/index.d.ts +68 -0
- package/index.js +31 -0
- package/license +9 -0
- package/package.json +27 -0
- package/readme.md +77 -0
package/README.md
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export interface Options {
|
|
2
|
+
/**
|
|
3
|
+
Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address. *(`false` matches any IP address in a string)*
|
|
4
|
+
|
|
5
|
+
@default false
|
|
6
|
+
*/
|
|
7
|
+
readonly exact?: boolean;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
Include boundaries in the regex. When `true`, `192.168.0.2000000000` will report as an invalid IPv4 address. If this option is not set, the mentioned IPv4 address would report as valid (ignoring the trailing zeros).
|
|
11
|
+
|
|
12
|
+
@default false
|
|
13
|
+
*/
|
|
14
|
+
readonly includeBoundaries?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare const ipRegex: {
|
|
18
|
+
/**
|
|
19
|
+
Regular expression for matching IP addresses.
|
|
20
|
+
|
|
21
|
+
@returns A regex for matching both IPv4 and IPv6.
|
|
22
|
+
|
|
23
|
+
@example
|
|
24
|
+
```
|
|
25
|
+
import ipRegex from 'ip-regex';
|
|
26
|
+
|
|
27
|
+
// Contains an IP address?
|
|
28
|
+
ipRegex().test('unicorn 192.168.0.1');
|
|
29
|
+
//=> true
|
|
30
|
+
|
|
31
|
+
// Is an IP address?
|
|
32
|
+
ipRegex({exact: true}).test('unicorn 192.168.0.1');
|
|
33
|
+
//=> false
|
|
34
|
+
|
|
35
|
+
'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex());
|
|
36
|
+
//=> ['192.168.0.1', '1:2:3:4:5:6:7:8']
|
|
37
|
+
|
|
38
|
+
// Contains an IP address?
|
|
39
|
+
ipRegex({includeBoundaries: true}).test('192.168.0.2000000000');
|
|
40
|
+
//=> false
|
|
41
|
+
|
|
42
|
+
// Matches an IP address?
|
|
43
|
+
'192.168.0.2000000000'.match(ipRegex({includeBoundaries: true}));
|
|
44
|
+
//=> null
|
|
45
|
+
```
|
|
46
|
+
*/
|
|
47
|
+
(options?: Options): RegExp;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
@returns A regex for matching IPv4.
|
|
51
|
+
*/
|
|
52
|
+
v4(options?: Options): RegExp;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
@returns A regex for matching IPv6.
|
|
56
|
+
|
|
57
|
+
@example
|
|
58
|
+
```
|
|
59
|
+
import ipRegex from 'ip-regex';
|
|
60
|
+
|
|
61
|
+
ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8');
|
|
62
|
+
//=> true
|
|
63
|
+
```
|
|
64
|
+
*/
|
|
65
|
+
v6(options?: Options): RegExp;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export default ipRegex;
|
package/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var word = "[a-fA-F\\d:]";
|
|
12
|
+
var boundry = function(options) {
|
|
13
|
+
return options && options.includeBoundaries ? "(?:(?<=\\s|^)(?=".concat(word, ")|(?<=").concat(word, ")(?=\\s|$))") : "";
|
|
14
|
+
};
|
|
15
|
+
var v4 = "(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}";
|
|
16
|
+
var v6segment = "[a-fA-F\\d]{1,4}";
|
|
17
|
+
var v6 = "\n(?:\n(?:".concat(v6segment, ":){7}(?:").concat(v6segment, "|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8\n(?:").concat(v6segment, ":){6}(?:").concat(v4, "|:").concat(v6segment, "|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4\n(?:").concat(v6segment, ":){5}(?::").concat(v4, "|(?::").concat(v6segment, "){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4\n(?:").concat(v6segment, ":){4}(?:(?::").concat(v6segment, "){0,1}:").concat(v4, "|(?::").concat(v6segment, "){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4\n(?:").concat(v6segment, ":){3}(?:(?::").concat(v6segment, "){0,2}:").concat(v4, "|(?::").concat(v6segment, "){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4\n(?:").concat(v6segment, ":){2}(?:(?::").concat(v6segment, "){0,3}:").concat(v4, "|(?::").concat(v6segment, "){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4\n(?:").concat(v6segment, ":){1}(?:(?::").concat(v6segment, "){0,4}:").concat(v4, "|(?::").concat(v6segment, "){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4\n(?::(?:(?::").concat(v6segment, "){0,5}:").concat(v4, "|(?::").concat(v6segment, "){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4\n)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1\n").replace(/\s*\/\/.*$/gm, "").replace(/\n/g, "").trim();
|
|
18
|
+
// Pre-compile only the exact regexes because adding a global flag make regexes stateful
|
|
19
|
+
var v46Exact = new RegExp("(?:^".concat(v4, "$)|(?:^").concat(v6, "$)"));
|
|
20
|
+
var v4exact = new RegExp("^".concat(v4, "$"));
|
|
21
|
+
var v6exact = new RegExp("^".concat(v6, "$"));
|
|
22
|
+
var ipRegex = function(options) {
|
|
23
|
+
return options && options.exact ? v46Exact : new RegExp("(?:".concat(boundry(options)).concat(v4).concat(boundry(options), ")|(?:").concat(boundry(options)).concat(v6).concat(boundry(options), ")"), "g");
|
|
24
|
+
};
|
|
25
|
+
ipRegex.v4 = function(options) {
|
|
26
|
+
return options && options.exact ? v4exact : new RegExp("".concat(boundry(options)).concat(v4).concat(boundry(options)), "g");
|
|
27
|
+
};
|
|
28
|
+
ipRegex.v6 = function(options) {
|
|
29
|
+
return options && options.exact ? v6exact : new RegExp("".concat(boundry(options)).concat(v6).concat(boundry(options)), "g");
|
|
30
|
+
};
|
|
31
|
+
var _default = ipRegex;
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@common.js/ip-regex",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "Regular expression for matching IP addresses (IPv4 & IPv6)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "etienne-martin/common.js",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"type": "commonjs",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "xo && ava && tsd"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"ava": "^3.15.0",
|
|
21
|
+
"tsd": "^0.19.1",
|
|
22
|
+
"xo": "^0.47.0"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
25
|
+
"dependencies": {},
|
|
26
|
+
"main": "./index.js"
|
|
27
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# ip-regex
|
|
2
|
+
|
|
3
|
+
> Regular expression for matching IP addresses
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
$ npm install ip-regex
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This module targets Node.js 12 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, use version 2.1.0: `npm install ip-regex@2.1.0`
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import ipRegex from 'ip-regex';
|
|
17
|
+
|
|
18
|
+
// Contains an IP address?
|
|
19
|
+
ipRegex().test('unicorn 192.168.0.1');
|
|
20
|
+
//=> true
|
|
21
|
+
|
|
22
|
+
// Is an IP address?
|
|
23
|
+
ipRegex({exact: true}).test('unicorn 192.168.0.1');
|
|
24
|
+
//=> false
|
|
25
|
+
|
|
26
|
+
ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8');
|
|
27
|
+
//=> true
|
|
28
|
+
|
|
29
|
+
'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex());
|
|
30
|
+
//=> ['192.168.0.1', '1:2:3:4:5:6:7:8']
|
|
31
|
+
|
|
32
|
+
// Contains an IP address?
|
|
33
|
+
ipRegex({includeBoundaries: true}).test('192.168.0.2000000000');
|
|
34
|
+
//=> false
|
|
35
|
+
|
|
36
|
+
// Matches an IP address?
|
|
37
|
+
'192.168.0.2000000000'.match(ipRegex({includeBoundaries: true}));
|
|
38
|
+
//=> null
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
### ipRegex(options?)
|
|
44
|
+
|
|
45
|
+
Returns a regex for matching both IPv4 and IPv6.
|
|
46
|
+
|
|
47
|
+
### ipRegex.v4(options?)
|
|
48
|
+
|
|
49
|
+
Returns a regex for matching IPv4.
|
|
50
|
+
|
|
51
|
+
### ipRegex.v6(options?)
|
|
52
|
+
|
|
53
|
+
Returns a regex for matching IPv6.
|
|
54
|
+
|
|
55
|
+
#### options
|
|
56
|
+
|
|
57
|
+
Type: `object`
|
|
58
|
+
|
|
59
|
+
##### exact
|
|
60
|
+
|
|
61
|
+
Type: `boolean`\
|
|
62
|
+
Default: `false` *(Matches any IP address in a string)*
|
|
63
|
+
|
|
64
|
+
Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address.
|
|
65
|
+
|
|
66
|
+
##### includeBoundaries
|
|
67
|
+
|
|
68
|
+
Type: `boolean`\
|
|
69
|
+
Default: `false`
|
|
70
|
+
|
|
71
|
+
Include boundaries in the regex. When `true`, `192.168.0.2000000000` will report as an invalid IPv4 address. If this option is not set, the mentioned IPv4 address would report as valid (ignoring the trailing zeros).
|
|
72
|
+
|
|
73
|
+
## Related
|
|
74
|
+
|
|
75
|
+
- [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address
|
|
76
|
+
- [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation
|
|
77
|
+
- [cidr-regex](https://github.com/silverwind/cidr-regex) - Regular expression for matching IP addresses in CIDR notation
|