JavaScript / ECMAScript

IPv4 Address in JS

Match valid IPv4 addresses with each octet constrained to 0–255.

Try it in the JS tester →

Pattern

regexJS
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("(?:(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)", "g");
const input = "192.168.1.1";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

Each octet alternation covers 250-255, 200-249, and 0-199 ranges to ensure strict 0-255 validity. Three octets with dots are matched, then the final octet.

Examples

Input

192.168.1.1

Matches

  • 192.168.1.1

Input

255.255.255.0

Matches

  • 255.255.255.0

Input

999.999.999.999

No match

Same pattern, other engines

← Back to IPv4 Address overview (all engines)