npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@chaisser/ip-regex

v1.0.0

Published

Validate and parse IP addresses (IPv4 and IPv6)

Readme

🌐 @chaisser/ip-regex

Validate and parse IP addresses (IPv4/IPv6) with regex, extraction, subnets, and more


✨ Features

  • 🎯 Type-safe - Full TypeScript support
  • Validation - IPv4, IPv6, and combined checks
  • 🔍 Extraction - Pull IPs from arbitrary text
  • 🔢 Conversion - IPv4 to/from numbers and buffers
  • 🏠 Special ranges - Detect private, loopback, multicast, link-local
  • 📐 Subnet checks - CIDR matching and subnet masks
  • 🔗 Regex exports - Use patterns directly
  • 🪶 Zero dependencies - Lightweight and tree-shakeable
  • 🏎️ ESM + CJS - Dual module format support

📦 Installation

npm install @chaisser/ip-regex
# or
yarn add @chaisser/ip-regex
# or
pnpm add @chaisser/ip-regex

🚀 Quick Start

import { isIPv4, isIPv6, isIP, isInSubnet } from '@chaisser/ip-regex';

isIPv4('192.168.1.1');       // true
isIPv6('::1');                // true
isIP('2001:db8::1');          // true

isInSubnet('192.168.1.42', '192.168.1.0/24'); // true

📖 What It Does

This package provides IP address validation and parsing for JavaScript and TypeScript. It validates IPv4 and IPv6 addresses using regex, extracts IPs from text, converts between formats, detects special ranges (private, loopback, multicast, link-local), and performs subnet/CIDR matching.


💡 Usage Examples

Validation

import { isIPv4, isIPv6, isIP } from '@chaisser/ip-regex';

isIPv4('0.0.0.0');              // true
isIPv4('255.255.255.255');      // true
isIPv4('256.0.0.1');            // false

isIPv6('2001:db8::1');          // true
isIPv6('::1');                   // true
isIPv6('::ffff:192.168.1.1');   // true (mapped)

isIP('192.168.1.1');            // true (IPv4)
isIP('::1');                     // true (IPv6)
isIP('hello');                   // false

Parsing

import { parseIP } from '@chaisser/ip-regex';

parseIP('192.168.1.1');  // { version: 4, address: '192.168.1.1' }
parseIP('::1');          // { version: 6, address: '::1' }
parseIP('invalid');      // null

Extract from Text

import { extractIPv4, extractIPv6, extractIP } from '@chaisser/ip-regex';

extractIPv4('Connect to 192.168.1.1 or 10.0.0.1');
// ['192.168.1.1', '10.0.0.1']

extractIP('Server at 10.0.0.1 and ::1');
// ['10.0.0.1', '::1']

IPv4 Conversions

import { ipv4ToNumber, numberToIPv4, ipv4ToBuffer, bufferToIPv4 } from '@chaisser/ip-regex';

ipv4ToNumber('192.168.1.1');  // 3232235777
numberToIPv4(3232235777);     // '192.168.1.1'

ipv4ToBuffer('10.0.0.1');     // Uint8Array [10, 0, 0, 1]
bufferToIPv4(new Uint8Array([10, 0, 0, 1])); // '10.0.0.1'

Special Ranges

import { isPrivateIPv4, isLoopbackIPv4, isMulticastIPv4, isLinkLocalIPv4 } from '@chaisser/ip-regex';

isPrivateIPv4('192.168.1.1');    // true
isPrivateIPv4('8.8.8.8');        // false

isLoopbackIPv4('127.0.0.1');     // true
isMulticastIPv4('224.0.0.1');    // true
isLinkLocalIPv4('169.254.0.1');  // true

Subnet / CIDR

import { isInSubnet, subnetMask } from '@chaisser/ip-regex';

isInSubnet('192.168.1.42', '192.168.1.0/24');  // true
isInSubnet('10.0.5.100', '10.0.0.0/16');       // true
isInSubnet('1.2.3.4', '0.0.0.0/0');            // true (matches all)

subnetMask(24);  // '255.255.255.0'
subnetMask(16);  // '255.255.0.0'
subnetMask(0);   // '0.0.0.0'

📚 API Reference

Regex Patterns

| Export | Type | Description | |---|---|---| | ipv4Regex | RegExp | Matches valid IPv4 strings | | ipv6Regex | RegExp | Matches valid IPv6 strings | | ipRegex | RegExp | Matches IPv4 or IPv6 |

Validation

| Function | Signature | Description | |---|---|---| | isIPv4(input) | (string) → boolean | Check if valid IPv4 | | isIPv6(input) | (string) → boolean | Check if valid IPv6 | | isIP(input) | (string) → boolean | Check if valid IPv4 or IPv6 |

Parsing

| Function | Signature | Description | |---|---|---| | parseIP(input) | (string) → ParsedIP \| null | Parse with version detection |

Extraction

| Function | Signature | Description | |---|---|---| | extractIPv4(input) | (string) → string[] | Extract IPv4 from text | | extractIPv6(input) | (string) → string[] | Extract IPv6 from text | | extractIP(input) | (string) → string[] | Extract all IPs from text |

IPv4 Conversions

| Function | Signature | Description | |---|---|---| | ipv4ToNumber(ip) | (string) → number | IPv4 to 32-bit integer | | numberToIPv4(num) | (number) → string | 32-bit integer to IPv4 | | ipv4ToBuffer(ip) | (string) → Uint8Array | IPv4 to byte array | | bufferToIPv4(buf) | (Uint8Array) → string | Byte array to IPv4 |

Special Ranges

| Function | Description | |---|---| | isPrivateIPv4(ip) | 10.x, 172.16-31.x, 192.168.x | | isLoopbackIPv4(ip) | 127.x.x.x | | isMulticastIPv4(ip) | 224-239.x.x.x | | isLinkLocalIPv4(ip) | 169.254.x.x |

Subnet

| Function | Signature | Description | |---|---|---| | isInSubnet(ip, cidr) | (string, string) → boolean | Check if IP is in CIDR range | | subnetMask(prefix) | (number) → string | Get subnet mask for prefix length |


🔗 Related Packages

Explore our other utility packages in the @chaisser namespace:


🔒 License

MIT - Free to use in personal and commercial projects


👨 Developed by

Doruk Karaboncuk [email protected]


📄 Repository


🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

📞 Support

For issues, questions, or suggestions, please reach out through:


Made with ❤️ by @chaisser