You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.2 KiB

4 years ago
  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)