Pattern library/IPv4 address regex

IPv4 address regex

Validates four octets in the 0–255 range—better than a naive \d+\.\d+ pattern that accepts 999.1.1.1.

Expression

/\b(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\b/g

How to read it

  • (?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)One octet 0–255

Sample test

3 match(es)
Servers at 192.168.1.1, 10.0.0.42, and 8.8.8.8. Invalid: 999.1.1.1 and 1.2.3.

Starter prompt: match IPv4 addresses