Regex for IPv4 address (0–255 per octet)

Matching four groups of digits is easy, but 999.1.1.1 would pass. This pattern limits each octet to the range 0 to 255.

The pattern

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

The tester opens with this pattern and the examples below already loaded. Nothing you type is uploaded.

How it works

25[0-5]
Octets 250 to 255.
2[0-4]\d
Octets 200 to 249.
1?\d?\d
Octets 0 to 199, with optional leading digits.
(?:...\.){3}
Three octets each followed by a dot, then a final octet without one.

Examples

Matches

  • 192.168.1.1
  • 10.0.0.255
  • 255.255.255.255
  • 8.8.8.8

Does not match

  • 999.1.1.1
  • 192.168.1
  • abc.def.ghi.jkl

Use it in JavaScript

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

// Find every match in a block of text
const found = text.match(re);

// Validate that a whole value matches (no stray characters around it)
const exact = new RegExp('^(?:' + re.source + ')$', re.flags.replace('g', ''));
const isValid = exact.test(value);

Limits and tips

  • Leading zeros such as 01.02.03.04 are accepted. Reject them in code if you need strict dotted-decimal form.
  • In a longer dotted string such as 1.2.3.4.5, the first four groups still match. Add lookarounds if that matters.
  • This covers IPv4 only. IPv6 needs a separate, much longer pattern.

Frequently asked questions

Why not just use \d{1,3}?

Because it accepts values like 999. Restricting each octet to 0–255 requires the alternation used here.

Does this match IPv6?

No. IPv6 addresses use hexadecimal groups and colons and need their own pattern.