@chaisser/ip-regex
v1.0.0
Published
Validate and parse IP addresses (IPv4 and IPv6)
Maintainers
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'); // falseParsing
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'); // nullExtract 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'); // trueSubnet / 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:
- @chaisser/ip-regex (this package) - Validate IP addresses (IPv4/IPv6)
- @chaisser/chunk-array - Split arrays into chunks
- @chaisser/string-wizard - Advanced string manipulation
- @chaisser/type-guard - Runtime type guards and validators
- @chaisser/uuid-v7 - Time-ordered UUID v7 generator
- @chaisser/wait-for - Promise-based wait utilities
- @chaisser/regex-humanizer - Regex to human-readable descriptions
- @chaisser/password-strength - Password strength checker
- @chaisser/human-time - Human-readable time formatting
- @chaisser/obj-path - Safe dot-notation object access
- @chaisser/debounce-throttle - Rate limiting utilities
- @chaisser/color-utils - Color conversion utilities
- @chaisser/deep-clone - Deep cloning functions
- @chaisser/array-group-by - Array grouping utilities
- @chaisser/merge-objects - Object merge utilities
- @chaisser/event-emitter - Typed event emitter
- @chaisser/unique-by - Array uniqueness utilities
- @chaisser/memoize - Function memoization
- @chaisser/base64-url - URL-safe base64 encoding
🔒 License
MIT - Free to use in personal and commercial projects
👨 Developed by
Doruk Karaboncuk [email protected]
📄 Repository
- GitHub: @Chaisser
- NPM: @chaisser/ip-regex
🤝 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:
- Email: [email protected]
- GitHub Issues: Create an issue
Made with ❤️ by @chaisser
