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
|