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]?):\d+$

Matches a valid IPv4 address followed by a colon and a port number (1-5 digits). The IPv4 address must be in dotted-decimal format with each octet between 0-255.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
(?: Non-capturing group start Start of group
(?: Non-capturing group start (inner) Start of inner group
25[0-5] Matches numbers 250-255 250, 255
| Alternation (OR operator) Separates alternatives
2[0-4][0-9] Matches numbers 200-249 200, 249
| Alternation (OR operator) Separates alternatives
[01]?[0-9][0-9]? Matches numbers 0-199 (with optional leading 0) 0, 99, 199
) Non-capturing group end (inner) End of inner group
\. Escaped literal dot (matches '.') .
){3} Quantifier: repeat the preceding group exactly 3 times Repeats group 3 times
(?: Non-capturing group start (outer) Start of outer group
25[0-5] Matches numbers 250-255 250, 255
| Alternation (OR operator) Separates alternatives
2[0-4][0-9] Matches numbers 200-249 200, 249
| Alternation (OR operator) Separates alternatives
[01]?[0-9][0-9]? Matches numbers 0-199 (with optional leading 0) 0, 99, 199
) Non-capturing group end (outer) End of outer group
: Literal colon :
\d+ Matches one or more digits (0-9) 80, 443, 65535
) Non-capturing group end End of group
$ Anchor: asserts position at end of the string End of string

Matches

  • 192.168.1.1:80
  • 10.0.0.1:443
  • 255.255.255.255:65535
  • 0.0.0.0:0
  • 172.16.254.1:8080

Does not match

  • 256.1.2.3:80
  • 192.168.1.1:65536
  • 192.168.1:80
  • 192.168.1.1.1:80
  • 192.168.1.1:abc
  • 192.168.1.1
  • :80
  • 192.168.1.1:

Common pitfalls

  • Forgetting to escape the dot (.) in the IPv4 pattern, which would match any character instead of a literal dot.
  • Not anchoring the pattern with ^ and $, which could lead to partial matches within longer strings.
  • Assuming the port number can be more than 5 digits (e.g., 65536), which is invalid for standard ports.
  • Overlooking the optional leading zero in octets (e.g., 010 is treated as 10, not 8 in octal).
  • Not accounting for the non-capturing groups, which could lead to unnecessary capturing overhead.

Performance notes

The pattern is well-structured and avoids catastrophic backtracking due to the use of non-capturing groups and quantifiers. However, the alternation with multiple nested groups may slightly impact performance for very long strings. Anchoring with ^ and $ ensures the entire string is validated, preventing partial matches.

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 capturing of substrings. Since the groups are only used for alternation and repetition, there is no need to capture their content.

Can this pattern match IPv6 addresses?

No, this pattern is specifically designed for IPv4 addresses in dotted-decimal format followed by a port number. IPv6 addresses have a different format and would require a separate pattern.

Why is the octet pattern [01]?[0-9][0-9]? used instead of a simpler range?

The pattern [01]?[0-9][0-9]? matches numbers from 0 to 199. The [01]? part allows for an optional leading 0 or 1, while [0-9][0-9]? matches the remaining digits. This ensures numbers like 0, 99, and 199 are matched correctly without allowing invalid numbers like 256.

Does this pattern validate the port number range (0-65535)?

No, this pattern only ensures the port number consists of one or more digits (\d+). It does not validate that the port number is within the valid range of 0-65535. Additional validation would be needed for that.