Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a valid IPv4 address (e.g., 192.168.1.1) with each octet between 0 and 255, ensuring no leading zeros unless the octet is a single digit.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of '192.168.1.1'
(?: Non-capturing group start (for grouping without capturing) Start of the first octet group
(?: Non-capturing group start (for grouping without capturing) Start of the first octet pattern
25[0-5] Matches numbers 250-255 250, 255
| Alternation (OR operator) Separates 25[0-5] from 2[0-4][0-9]
2[0-4][0-9] Matches numbers 200-249 200, 249
| Alternation (OR operator) Separates 2[0-4][0-9] from [01]?[0-9][0-9]?
[01]?[0-9][0-9]? Matches numbers 0-199 with optional leading zero 0, 1, 10, 99, 199
) End of non-capturing group (for the first octet pattern) End of the first octet pattern
\. Escaped dot: matches a literal dot (.) The dot in '192.168.1.1'
){3} Quantifier: matches the preceding group exactly 3 times The three dots in '192.168.1.1'
(?: Non-capturing group start (for the last octet pattern) Start of the last octet pattern
25[0-5] Matches numbers 250-255 250, 255
| Alternation (OR operator) Separates 25[0-5] from 2[0-4][0-9]
2[0-4][0-9] Matches numbers 200-249 200, 249
| Alternation (OR operator) Separates 2[0-4][0-9] from [01]?[0-9][0-9]?
[01]?[0-9][0-9]? Matches numbers 0-199 with optional leading zero 0, 1, 10, 99, 199
) End of non-capturing group (for the last octet pattern) End of the last octet pattern
$ Anchor: asserts the position at the end of the string End of '192.168.1.1'

Matches

  • 0.0.0.0
  • 1.2.3.4
  • 192.168.1.1
  • 255.255.255.255
  • 10.0.0.1

Does not match

  • 256.1.1.1
  • 192.168.1
  • 192.168.1.1.1
  • 192.168.01.1
  • 1.2.3.4.5
  • abc.def.ghi.jkl
  • 192.168.1.01

Common pitfalls

  • Forgetting to escape the dot (.) in the regex, which would match any character instead of a literal dot.
  • Not accounting for leading zeros in octets (e.g., '01' is invalid unless it's a single digit like '1').
  • Overlooking the requirement for exactly 4 octets separated by dots.
  • Assuming the regex matches all valid IPv4 addresses without considering edge cases like '0.0.0.0' or '255.255.255.255'.

Performance notes

The regex is efficient for its purpose as it uses non-capturing groups and avoids catastrophic backtracking. However, it is not optimized for very long strings (e.g., 1MB+), where simpler patterns or native IP validation might be preferable.

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 use non-capturing groups (?:...) instead of capturing groups (...)?

Non-capturing groups are used here to improve performance by avoiding unnecessary capture of substrings, as the matched octets are not referenced later in the pattern.

Does this regex handle all valid IPv4 addresses?

Yes, it matches all valid IPv4 addresses where each octet is between 0 and 255, including edge cases like '0.0.0.0' and '255.255.255.255'. It does not match invalid formats like '256.1.1.1' or '192.168.01.1'.

Can this regex be used in all regex flavors?

The regex is written in JavaScript flavor, which supports non-capturing groups (?:...). Most modern regex flavors (e.g., PCRE, Python, Java) support this syntax, but some older or less common flavors may not.

How would I modify this regex to match IPv4 addresses in a larger string?

Remove the ^ and $ anchors to allow matching IPv4 addresses embedded in other text. For example: (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).