cidr-regex

Regular expression for matching IP addresses in CIDR notation

BSD-2-Clause 36 个版本
安装
npm install cidr-regex
yarn add cidr-regex
pnpm add cidr-regex
bun add cidr-regex
README

cidr-regex

Regular expression for matching IP addresses in CIDR notation

Usage

$ npm i cidr-regex
import cidrRegex from "cidr-regex";

// Contains a CIDR IP address?
cidrRegex().test("foo 192.168.0.1/24");
//=> true

// Is a CIDR IP address?
cidrRegex({exact: true}).test("foo 192.168.0.1/24");
//=> false

cidrRegex.v6({exact: true}).test("1:2:3:4:5:6:7:8/64");
//=> true

// Extract CIDRs from string
"foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz".match(cidrRegex());
//=> ["192.168.0.1/24", "1:2:3:4:5:6:7:8/64"]

API

cidrRegex(options?: CidrRegexOptions)

Returns a regex for matching both IPv4 and IPv6 CIDR IP addresses.

cidrRegex.v4(options?: CidrRegexOptions)

Returns a regex for matching IPv4 CIDR IP addresses.

cidrRegex.v6(options?: CidrRegexOptions)

Returns a regex for matching IPv6 CIDR IP addresses.

CidrRegexOptions

The options object has the following properties:

  • exact boolean: Only match an exact string. Useful with RegExp#test() to check if a string is a CIDR IP address. Default: false.
  • ip-bigint - Convert IPv4 and IPv6 addresses to native BigInt and vice-versa
  • ip-regex - Regular expression for matching IP addresses
  • is-cidr - Check if a string is an IP address in CIDR notation
  • is-ip - Check if a string is an IP address
  • cidr-tools - Tools to work with IPv4 and IPv6 CIDR network lists

License

© silverwind, distributed under BSD licence

Based on previous work by Felipe Apostol

版本列表
6.0.0 2026-05-07
5.0.5 2026-04-28
5.0.4 2026-04-21
5.0.3 2026-02-13
5.0.2 2026-02-09
5.0.1 2025-09-23
5.0.0 2025-08-01
4.1.3 2025-02-17
4.1.2 2025-02-14
4.1.1 2024-05-23
4.1.0 2024-05-23
4.0.5 2024-04-09
4.0.4 2024-04-08
4.0.3 2023-03-30
4.0.2 2022-09-20
4.0.1 2022-09-19
4.0.0 2022-09-16
3.1.1 2020-09-28
3.1.0 2020-09-08
3.0.0 2020-05-13
2.0.10 2018-09-18
2.0.9 2018-06-04
2.0.8 2018-03-03
2.0.7 2017-12-19
2.0.6 2017-12-18
2.0.5 2017-12-18
2.0.4 2017-12-18
2.0.3 2017-12-18
2.0.2 2017-12-18
2.0.1 2017-12-18
2.0.0 2017-12-18
1.0.7 2017-11-12
1.0.6 2016-03-24
1.0.5 2016-03-24
1.0.2 2016-03-24
1.0.0 2016-03-24