Match IPv4 addresses with regex
A loose IPv4 pattern that catches dotted-quads and a strict variant that rejects 999.999.999.999. Pick the one that matches your validation budget.
# pattern
/\b(?:\d{1,3}\.){3}\d{1,3}\b/g → Open in regex tester (pre-filled)
# how it works
Three groups of one-to-three digits followed by dots, then one final group. Word boundaries (`\b`) prevent matching inside longer strings. This is the *loose* form — it accepts 999.999.999.999.
# sample input
router 192.168.1.1 dns 8.8.8.8 garbage 999.999.999.999 internal 10.0.0.1
# pitfalls
- The strict form bounding each octet to 0-255 is much longer: (25[0-5]|2[0-4]\d|[01]?\d?\d) repeated 4× with dots. Only worth it when the input is hostile.
- Does not validate that the address is routable, private, or actually assigned. That is a network question, not a regex one.
- IPv6 is a different grammar entirely. Don't try to merge them into one pattern.