1 | # cidr-regex
|
2 | [![](https://img.shields.io/npm/v/cidr-regex.svg?style=flat)](https://www.npmjs.org/package/cidr-regex) [![](https://img.shields.io/npm/dm/cidr-regex.svg)](https://www.npmjs.org/package/cidr-regex) [![](https://api.travis-ci.org/silverwind/cidr-regex.svg?style=flat)](https://travis-ci.org/silverwind/cidr-regex)
|
3 |
|
4 | > Regular expression for matching IP addresses in CIDR notation
|
5 |
|
6 | ## Install
|
7 |
|
8 | ```sh
|
9 | $ npm install --save cidr-regex
|
10 | ```
|
11 |
|
12 | ## Usage
|
13 |
|
14 | ```js
|
15 | const cidrRegex = require('cidr-regex');
|
16 |
|
17 | // Contains a CIDR IP address?
|
18 | cidrRegex().test('foo 192.168.0.1/24');
|
19 | //=> true
|
20 |
|
21 | // Is a CIDR IP address?
|
22 | cidrRegex({exact: true}).test('foo 192.168.0.1/24');
|
23 | //=> false
|
24 |
|
25 | cidrRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8/64');
|
26 | //=> true
|
27 |
|
28 | 'foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz'.match(cidrRegex());
|
29 | //=> ['192.168.0.1/24', '1:2:3:4:5:6:7:8/64']
|
30 | ```
|
31 |
|
32 | ## API
|
33 |
|
34 | ### cidrRegex([options])
|
35 |
|
36 | Returns a regex for matching both IPv4 and IPv6 CIDR IP addresses.
|
37 |
|
38 | ### cidrRegex.v4([options])
|
39 |
|
40 | Returns a regex for matching IPv4 CIDR IP addresses.
|
41 |
|
42 | ### cidrRegex.v6([options])
|
43 |
|
44 | Returns a regex for matching IPv6 CIDR IP addresses.
|
45 |
|
46 | #### options.exact
|
47 |
|
48 | Type: `boolean`<br>
|
49 | Default: `false` *(Matches any CIDR IP address in a string)*
|
50 |
|
51 | Only match an exact string. Useful with `RegExp#test()` to check if a string is a CIDR IP address.
|
52 |
|
53 |
|
54 | ## Related
|
55 |
|
56 | - [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation
|
57 | - [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address
|
58 | - [ip-regex](https://github.com/sindresorhus/ip-regex) - Regular expression for matching IP addresses
|
59 |
|
60 | ## License
|
61 |
|
62 | © [silverwind](https://github.com/silverwind), distributed under BSD licence
|
63 |
|
64 | Based on previous work by [Felipe Apostol](https://github.com/flipjs)
|