Regex tester

Regex for IPv4 addresses

A quick pattern like \d+\.\d+\.\d+\.\d+ also accepts 999.1.1.1. This one only allows each of the four parts to be 0 to 255, without leading zeros:

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

Regex tester
Test textTEXT

Press Command or Control plus Enter to run. Tab inserts indentation; press Escape, then Tab, to leave the editor.

Result
Your result will appear here.
Ready8 lines · 85 chars

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.

AlternativeMatches
25[0-5]250 to 255
2[0-4]\d200 to 249
1\d\d100 to 199
[1-9]?\d0 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.

More regex tester guides

Open the full regex tester →

From the blog