IPv4 Address Regex
Validates a dotted-quad IPv4 address, ensuring each of the four octets is a number between 0 and 255 with no leading zeros.
Regex Pattern
^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| 25[0-5] | Matches 250-255 |
| 2[0-4][0-9] | Matches 200-249 |
| 1[0-9]{2} | Matches 100-199 |
| [1-9]?[0-9] | Matches 0-99 without a leading zero |
| \. | Literal dot separating octets |
| {3} | Repeats the dot-plus-octet group exactly three more times for a total of four octets |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern validates that a string is a syntactically correct IPv4 address in dotted-decimal notation, where each of the four octets is a number from 0 to 255. It rejects octets greater than 255, missing or extra octets, and octets with leading zeros.
Why it works
Each octet is matched by an alternation ordered from most to least specific: 250-255, then 200-249, then 100-199, then 0-99 without a leading zero. Because regex alternation tries branches left to right and the ranges don't overlap, exactly one branch matches any valid 0-255 value. The first octet is matched once, then the remaining three are matched via a repeated group of a literal dot followed by the same octet alternation, repeated exactly three times with {3}.
Common use cases
- Validating IP address input fields in network configuration forms
- Filtering log files for lines containing valid IPv4 addresses
- Sanity-checking values before passing them to socket or DNS APIs
- Distinguishing IPv4 literals from hostnames in mixed configuration files
Edge cases
- Values like 256.1.1.1 are correctly rejected because no branch of the octet alternation matches 256
- Leading zeros like 192.168.01.1 are rejected by design since [1-9]?[0-9] does not match two-digit numbers starting with 0
- 0.0.0.0 and 255.255.255.255 are valid boundary values and both match
- Extra segments like 192.168.1.1.1 are rejected because the pattern expects exactly four octets
Limitations
- Does not validate IPv6 addresses; a separate pattern is needed for that format
- Does not check whether the address is routable, private, or reserved (e.g. loopback, multicast ranges)
- Rejects the CIDR suffix (e.g. /24) so subnet notation needs its own pattern extension
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$/;
console.log(ipv4Regex.test('192.168.1.1')); // trueCommon Mistakes
Using a naive pattern like \d{1,3}(\.\d{1,3}){3} that accepts invalid octets like 999.999.999.999
Fix: Use the range-aware alternation (25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]) so each octet is constrained to 0-255
Allowing leading zeros in octets, which can be misread as octal by some parsers and create ambiguity
Fix: Use [1-9]?[0-9] instead of [0-9]{1,3} so a lone zero is allowed but 01 or 001 are rejected
Forgetting to anchor the pattern, causing it to match an IPv4-looking substring inside a longer, invalid string
Fix: Anchor with ^ and $ so the entire input must be a valid address
Performance Notes
- The octet alternation is ordered from longest/most specific match to shortest, which is efficient since the engine tries branches in order
- All quantifiers are bounded, so there is no catastrophic backtracking risk even on malformed long input
- The repeated group {3} avoids duplicating the octet alternation four times in the source pattern, keeping it maintainable
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |