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 in dotted-decimal notation (e.g., 192.168.1.1) where each octet is between 0 and 255.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '192.168.1.1'
( Start of a non-capturing group (for alternation) Group start before '25[0-5]'
25 Literal characters '2' followed by '5' Digits '25' in '255'
[0-5] Character class matching digits 0-5 Digit '5' in '255'
| Alternation operator (OR) Separates '25[0-5]' from '2[0-4][0-9]'
2 Literal character '2' Digit '2' in '249'
[0-4] Character class matching digits 0-4 Digit '4' in '249'
[0-9] Character class matching any digit 0-9 Digit '9' in '249'
| Alternation operator (OR) Separates '2[0-4][0-9]' from '[01]?[0-9][0-9]?'
[01]? Optional character class matching '0' or '1' (0 or 1 occurrence) Digit '0' or '1' in '099' or '192'
[0-9] Character class matching any digit 0-9 Digit '9' in '192'
[0-9]? Optional digit (0 or 1 occurrence) Digit '2' in '192' (optional in '92')
) End of non-capturing group Group end after '[01]?[0-9][0-9]?'
\. Escaped literal dot (matches '.') Dot between octets in '192.168.1.1'
( Start of a non-capturing group (for repetition) Group start before '25[0-5]'
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) Same octet pattern as before (reused for repetition) Octet like '255' or '192'
\. Escaped literal dot (matches '.') Dot in '192.168.1.1'
){2} Quantifier: repeat the preceding group exactly 2 times Matches two octet-dot pairs (e.g., '.168.1')
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) Final octet pattern (same as first) Last octet like '1' in '192.168.1.1'
$ Anchor: asserts position at end of the string End of '192.168.1.1'

Matches

  • 192.168.1.1
  • 0.0.0.0
  • 255.255.255.255
  • 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
  • 192.168.-1.1

Common pitfalls

  • Fails to match leading zeros in octets (e.g., '192.168.001.001' is invalid in strict mode).
  • Does not account for whitespace (e.g., ' 192.168.1.1 ' fails).
  • Overly permissive with optional leading digits (e.g., '999' is invalid but may match if not anchored properly).
  • Does not validate the entire string (e.g., '192.168.1.1extra' matches the first part but not the whole string).

Performance notes

The pattern is prone to catastrophic backtracking if not anchored properly (e.g., without ^ and $). The alternation and optional quantifiers can create many paths, but the anchors mitigate this. For very long strings, consider optimizing or using a dedicated IP validation library.

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

Why does the pattern use [01]?[0-9][0-9]? instead of [0-9]{1,3}?

The pattern ensures each octet is between 0 and 255 by breaking it into three cases: 250-255, 200-249, and 0-199. [0-9]{1,3} would allow invalid octets like '999' or '000' (if leading zeros are disallowed).

Can this pattern match IPv4 addresses with leading zeros (e.g., 192.168.001.001)?

No, the pattern does not explicitly allow leading zeros beyond the optional [01]? (e.g., '001' would not match because [01]? only allows 0 or 1 once). Strictly speaking, leading zeros are invalid in standard IPv4 notation.

Is this pattern safe for use in JavaScript?

Yes, but ensure the string is trimmed of whitespace before matching, as the pattern does not account for leading/trailing spaces. Also, avoid using it on very long strings to prevent performance issues.