Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a valid IPv4 address (0.0.0.0 to 255.255.255.255) with strict octet validation.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: start of string Start of '192.168.1.1'
(?: Non-capturing group start Grouping for alternation
25[0-5] Matches 250-255 255
| Alternation (OR) Separates 25[0-5] from 2[0-4][0-9]
2[0-4][0-9] Matches 200-249 249
| Alternation (OR) Separates 2[0-4][0-9] from [01]?[0-9][0-9]?
[01]? Optional 0 or 1 0 or 1 (e.g., '0' or '1')
[0-9] Digit 0-9 Any digit
[0-9]? Optional digit Optional second digit (e.g., '5' or '55')
) Non-capturing group end End of octet group
\. Escaped dot (literal '.') .
(?: Non-capturing group start (repeated) Grouping for repeated octets
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) Same octet pattern as before 255
\. Escaped dot (literal '.') .
){2} Repeat group exactly 2 times Matches two octet-dot pairs
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) Final octet (no trailing dot) 1
) Non-capturing group end End of full pattern
$ Anchor: end of string End of '192.168.1.1'

Matches

  • 0.0.0.0
  • 255.255.255.255
  • 192.168.1.1
  • 10.0.0.1
  • 172.16.254.1

Does not match

  • 256.1.1.1
  • 192.168.1
  • 192.168.1.1.1
  • 192.168.01.1
  • 192.168.1.1a
  • 1.2.3.04

Common pitfalls

  • Leading zeros in octets (e.g., '010.0.0.1') are technically invalid in strict IPv4 but may pass in some implementations.
  • Missing anchors (^/$) could allow partial matches (e.g., '192.168.1.1abc').
  • Over-reliance on this pattern for validation without additional checks (e.g., leading zeros).
  • Assuming this matches all valid IPv4 addresses (e.g., it does not handle edge cases like '0.0.0.0' with leading zeros).

Performance notes

Low risk of catastrophic backtracking due to fixed-length alternations and no nested quantifiers. The pattern is efficient for typical use cases.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this pattern allow leading zeros in octets?

Yes, but strictly speaking, leading zeros are not standard in IPv4 (e.g., '010.0.0.1' is technically invalid). This pattern allows them due to [01]?[0-9][0-9]?.

Can this pattern match IPv4 addresses with extra whitespace?

No. The anchors (^/$) ensure the entire string is a valid IPv4 address without leading/trailing whitespace.

Is this pattern RFC-compliant for IPv4?

No. RFC 791 allows leading zeros, but this pattern does not enforce strict octet validation (e.g., '010' is treated as '10').

How would I modify this to reject leading zeros?

Replace [01]?[0-9][0-9]? with (?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) to disallow leading zeros.