How one part (an octet) is matched
The first group repeats three times with a dot after it, then a last octet without a dot.
| Alternative | Matches |
|---|---|
25[0-5] | 250 to 255 |
2[0-4]\d | 200 to 249 |
1\d\d | 100 to 199 |
[1-9]?\d | 0 to 99, without a leading zero |
Why reject leading zeros
Some software reads 010 as octal, so 192.168.010.1 can mean 192.168.8.1 to one program and 192.168.10.1 to another. That mismatch has caused real security bugs in access checks, so most validators reject leading zeros.
In Node.js, use the built-in check
import { isIP, isIPv4, isIPv6 } from "node:net";
isIPv4("192.168.1.1"); // true
isIPv4("256.1.1.1"); // false
isIPv6("2001:db8::1"); // true
isIP("example.com"); // 0 (not an IP address)Frequently asked questions
Does this regex match IPv6 addresses?
No. IPv6 has many valid shortened forms, so a regex for it is long and error-prone. Use isIPv6 in Node.js or an IP address library instead.
How do I find IP addresses inside log lines?
Remove ^ and $ and wrap the pattern in \b word boundaries, then use the g flag to find every address in the text.
Is 0.0.0.0 a valid IPv4 address?
It is valid syntax and matches. It has a special meaning, though: "all addresses on this machine" when a server listens on it, so you may want to reject it as a target.