Regex for IP Address Validation
Test regex patterns for IPv4 and IPv6 address validation with real-time match highlighting.
Open Regex Tester →IP Address Regex Patterns
The basic IPv4 pattern
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b matches the dot-decimal format but allows invalid octets like 999. A stricter version validates each octet: \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b. IPv6 is much more complex due to its hexadecimal notation and zero-compression (::) — a full IPv6 regex is typically 200+ characters.IPv4 vs IPv6 Validation
- IPv4: 4 octets (0-255) separated by dots. 32 bits total. Example: 192.168.1.1
- IPv6: 8 groups of 4 hex digits separated by colons. 128 bits. Example: 2001:0db8:85a3::8a2e:0370:7334
- CIDR notation: Append /prefix for subnets — e.g., 10.0.0.0/8 or 2001:db8::/32
For production validation, prefer standard library functions over regex: Python's ipaddress module, Node's net.isIP(), or Go's net.ParseIP().
// IPv4 (basic)
/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
// IPv4 (strict - validates 0-255)
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/
// JavaScript - use net module
const net = require('net');
net.isIP('192.168.1.1'); // 4
net.isIP('::1'); // 6
net.isIP('invalid'); // 0