UNPKG

1.9 kBMarkdownView Raw
1# ip-regex
2
3> Regular expression for matching IP addresses
4
5## Install
6
7```sh
8$ npm install ip-regex
9```
10
11This 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
16import ipRegex from 'ip-regex';
17
18// Contains an IP address?
19ipRegex().test('unicorn 192.168.0.1');
20//=> true
21
22// Is an IP address?
23ipRegex({exact: true}).test('unicorn 192.168.0.1');
24//=> false
25
26ipRegex.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?
33ipRegex({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
45Returns a regex for matching both IPv4 and IPv6.
46
47### ipRegex.v4(options?)
48
49Returns a regex for matching IPv4.
50
51### ipRegex.v6(options?)
52
53Returns a regex for matching IPv6.
54
55#### options
56
57Type: `object`
58
59##### exact
60
61Type: `boolean`\
62Default: `false` *(Matches any IP address in a string)*
63
64Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address.
65
66##### includeBoundaries
67
68Type: `boolean`\
69Default: `false`
70
71Include 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