1 | # ip-regex
|
2 |
|
3 | > Regular expression for matching IP addresses
|
4 |
|
5 |
|
6 | ## Install
|
7 |
|
8 | ```
|
9 | $ npm install ip-regex
|
10 | ```
|
11 |
|
12 | This module targets Node.js 8 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`
|
13 |
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ```js
|
18 | const ipRegex = require('ip-regex');
|
19 |
|
20 | // Contains an IP address?
|
21 | ipRegex().test('unicorn 192.168.0.1');
|
22 | //=> true
|
23 |
|
24 | // Is an IP address?
|
25 | ipRegex({exact: true}).test('unicorn 192.168.0.1');
|
26 | //=> false
|
27 |
|
28 | ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8');
|
29 | //=> true
|
30 |
|
31 | 'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex());
|
32 | //=> ['192.168.0.1', '1:2:3:4:5:6:7:8']
|
33 |
|
34 | // Contains an IP address?
|
35 | ipRegex({includeBoundaries: true}).test('192.168.0.2000000000');
|
36 | //=> false
|
37 |
|
38 | // Matches an IP address?
|
39 | '192.168.0.2000000000'.match(ipRegex({includeBoundaries: true}));
|
40 | //=> null
|
41 | ```
|
42 |
|
43 |
|
44 | ## API
|
45 |
|
46 | ### ipRegex([options])
|
47 |
|
48 | Returns a regex for matching both IPv4 and IPv6.
|
49 |
|
50 | ### ipRegex.v4([options])
|
51 |
|
52 | Returns a regex for matching IPv4.
|
53 |
|
54 | ### ipRegex.v6([options])
|
55 |
|
56 | Returns a regex for matching IPv6.
|
57 |
|
58 | #### options
|
59 |
|
60 | Type: `Object`
|
61 |
|
62 | ##### exact
|
63 |
|
64 | Type: `boolean`<br>
|
65 | Default: `false` *(Matches any IP address in a string)*
|
66 |
|
67 | Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address.
|
68 |
|
69 | ##### includeBoundaries
|
70 |
|
71 | Type: `boolean`<br>
|
72 | Default: `false`
|
73 |
|
74 | 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).
|
75 |
|
76 |
|
77 | ## Related
|
78 |
|
79 | - [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address
|
80 | - [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation
|
81 | - [cidr-regex](https://github.com/silverwind/cidr-regex) - Regular expression for matching IP addresses in CIDR notation
|
82 |
|
83 |
|
84 | ## License
|
85 |
|
86 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|