index.d.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. declare namespace ip {
  2. interface Options {
  3. /**
  4. Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address. *(`false` matches any IP address in a string)*
  5. @default false
  6. */
  7. readonly exact?: boolean;
  8. /**
  9. 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).
  10. @default false
  11. */
  12. readonly includeBoundaries?: boolean;
  13. }
  14. }
  15. declare const ip: {
  16. /**
  17. Regular expression for matching IP addresses.
  18. @returns A regex for matching both IPv4 and IPv6.
  19. @example
  20. ```
  21. import ipRegex = require('ip-regex');
  22. // Contains an IP address?
  23. ipRegex().test('unicorn 192.168.0.1');
  24. //=> true
  25. // Is an IP address?
  26. ipRegex({exact: true}).test('unicorn 192.168.0.1');
  27. //=> false
  28. 'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex());
  29. //=> ['192.168.0.1', '1:2:3:4:5:6:7:8']
  30. // Contains an IP address?
  31. ipRegex({includeBoundaries: true}).test('192.168.0.2000000000');
  32. //=> false
  33. // Matches an IP address?
  34. '192.168.0.2000000000'.match(ipRegex({includeBoundaries: true}));
  35. //=> null
  36. ```
  37. */
  38. (options?: ip.Options): RegExp;
  39. /**
  40. @returns A regex for matching IPv4.
  41. */
  42. v4(options?: ip.Options): RegExp;
  43. /**
  44. @returns A regex for matching IPv6.
  45. @example
  46. ```
  47. import ipRegex = require('ip-regex');
  48. ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8');
  49. //=> true
  50. ```
  51. */
  52. v6(options?: ip.Options): RegExp;
  53. };
  54. export = ip;