e30da5c1b7cbb2e6fd7e36f21be555ed1a910134e9c9bab9504546f4cc677531ca762612c0351490b416ff8e1a3f7a923e70dd770633d1813eb66bb0fff5a0 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # ip-regex [![Build Status](https://travis-ci.org/sindresorhus/ip-regex.svg?branch=master)](https://travis-ci.org/sindresorhus/ip-regex)
  2. > Regular expression for matching IP addresses
  3. ## Install
  4. ```
  5. $ npm install --save ip-regex
  6. ```
  7. ## Usage
  8. ```js
  9. const ipRegex = require('ip-regex');
  10. // Contains an IP address?
  11. ipRegex().test('unicorn 192.168.0.1');
  12. //=> true
  13. // Is an IP address?
  14. ipRegex({exact: true}).test('unicorn 192.168.0.1');
  15. //=> false
  16. ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8');
  17. //=> true
  18. 'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex());
  19. //=> ['192.168.0.1', '1:2:3:4:5:6:7:8']
  20. ```
  21. ## API
  22. ### ipRegex([options])
  23. Returns a regex for matching both IPv4 and IPv6.
  24. ### ipRegex.v4([options])
  25. Returns a regex for matching IPv4.
  26. ### ipRegex.v6([options])
  27. Returns a regex for matching IPv6.
  28. #### options.exact
  29. Type: `boolean`<br>
  30. Default: `false` *(Matches any IP address in a string)*
  31. Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address.
  32. ## Related
  33. - [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address
  34. ## License
  35. MIT © [Sindre Sorhus](https://sindresorhus.com)