Back to all patterns
IPv4 Address Validation
Web & Network
Validates a standard dotted-decimal IPv4 address.
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/How it works
This regex ensures that the IP address consists of four groups of numbers separated by dots. Each group must be a valid number between 0 and 255. It correctly handles single-digit, double-digit, and triple-digit numbers within the valid range.
Test Cases
Should Match
- 192.168.1.1
- 255.255.255.0
- 127.0.0.1
Should NOT Match
- 256.1.1.1
- 192.168.1
- 192.168.1.1.1
Quick Summary
Validates that a string is a properly formatted IPv4 address in dotted-decimal notation, with each octet between 0 and 255.
Key Takeaways
Key Takeaways
- Validates all four octets are in the range 0–255
- Rejects addresses with more or fewer than four octets
- Does not validate whether the IP is routable or reserved (e.g., 0.0.0.0, 127.x.x.x)
- Use CIDR notation validation separately if you need to match subnets like 192.168.1.0/24
Use Cases
When to use it
- Validating IP address fields in network configuration forms
- Filtering log entries by IP address format
- Parsing firewall rule inputs
Watch out
Common Mistakes
- Forgetting that 0.0.0.0 and 255.255.255.255 are valid matches, add exclusions if needed
- Using a simpler pattern like \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} which allows 999.999.999.999
FAQ
IPv4 Address Validation, Frequently Asked
How do I also match IPv6 addresses?
IPv6 requires a separate, more complex pattern. Use a dedicated library like 'is-ip' in Node.js for robust multi-format IP validation.
Does this match CIDR notation like 192.168.1.0/24?
No. Append /\d{1,2}$ to the pattern to optionally match a CIDR prefix.