Regex for IPv4 Address
Validates IPv4 addresses (four octets from 0-255 separated by dots).
Pattern
/^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/ Live Tester
Enter a string to test
Examples
✓ 192.168.1.1
✓ 10.0.0.0
✓ 255.255.255.255
✗ 256.1.1.1
✗ 192.168.1
✗ 192.168.1.1.1
Pattern Breakdown
(25[0-5]|2[0-4]\d|[01]?\d\d?) — matches 0-255
\. — literal dot separator
{3} — first three octets with dots
final octet without trailing dot —
Code Snippets
Javascript
const regex = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
regex.test("192.168.1.1"); // true Python
import re
pattern = r"^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$"
bool(re.match(pattern, "192.168.1.1")) # True Php
$pattern = '/^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/';
preg_match($pattern, "192.168.1.1"); // 1 Related Patterns
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv6 Address /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
Frequently Asked Questions
What is the regex for IPv4 Address?▾
The regex pattern for IPv4 Address is /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/. Validates IPv4 addresses (four octets from 0-255 separated by dots).
How do I use the IPv4 Address regex in JavaScript?▾
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the IPv4 Address pattern match?▾
It matches strings like 192.168.1.1; it rejects strings like 256.1.1.1. See the Examples section above for the full list of matching and non-matching cases.