UNPKG

1.08 kBMarkdownView Raw
1# ip-regex [![Build Status](https://travis-ci.org/sindresorhus/ip-regex.svg?branch=master)](https://travis-ci.org/sindresorhus/ip-regex)
2
3> Regular expression for matching IP addresses
4
5
6## Install
7
8```sh
9$ npm install --save ip-regex
10```
11
12
13## Usage
14
15```js
16var ipRegex = require('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
33
34## API
35
36### ipRegex(options)
37
38Returns a regex for matching both IPv4 and IPv6.
39
40### ipRegex.v4(options)
41
42Returns a regex for matching IPv4.
43
44### ipRegex.v6(options)
45
46Returns a regex for matching IPv6.
47
48#### options.exact
49
50Type: `boolean`
51Default: `false` *(Matches any IP address in a string)*
52
53Only match an exact string.
54Useful with `RegExp#test` to check if a string is an IP address.
55
56
57## License
58
59MIT © [Sindre Sorhus](http://sindresorhus.com)