Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?:(?:\d{1,3}\.){3}\d{1,3})|(?:(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4})$

Matches either an IPv4 address or a full (uncompressed) IPv6 address from start to end of the string.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of input
(?: Non-capturing group start (outer alternation group) Start of either IPv4 or IPv6 pattern
(?: Non-capturing group start (IPv4 segment group) Start of IPv4 segment repetition
\d Digit character class (0-9) 0, 5, 9
{1,3} Quantifier: match between 1 and 3 of the preceding token (digits) 1, 12, 123
\. Literal dot character (escaped) .
){3} Quantifier: repeat the preceding group (digits followed by dot) exactly 3 times 192.168.1.
\d Digit character class (0-9) 0, 5, 9
{1,3} Quantifier: match between 1 and 3 of the preceding token (digits) 1, 12, 123
) End of IPv4 segment group End of IPv4 segment
| Alternation: separates two alternatives in the outer group OR operator between IPv4 and IPv6
(?: Non-capturing group start (IPv6 segment group) Start of IPv6 segment repetition
(?: Non-capturing group start (single IPv6 segment) Start of a single IPv6 segment
[0-9a-fA-F] Hexadecimal digit character class (case-insensitive) 0, a, F, 9
{1,4} Quantifier: match between 1 and 4 of the preceding token (hex digits) 0, ab, 1234, FFFF
: Literal colon character :
){7} Quantifier: repeat the preceding group (hex segment followed by colon) exactly 7 times 2001:0db8:85a3:0000:0000:8a2e:0370:7334:
[0-9a-fA-F] Hexadecimal digit character class (case-insensitive) 0, a, F, 9
{1,4} Quantifier: match between 1 and 4 of the preceding token (hex digits) 0, ab, 1234, FFFF
) End of IPv6 segment group End of IPv6 address
$ Anchor: asserts position at end of the string End of input

Matches

  • 192.168.1.1
  • 10.0.0.1
  • 2001:0db8:85a3:0000:0000:8a2e:0370:7334
  • 2001:db8:85a3:0:0:8a2e:370:7334
  • 0:0:0:0:0:0:0:1
  • 127.0.0.1

Does not match

  • 256.1.1.1
  • 192.168.1
  • 192.168.1.1.1
  • 2001:0db8:85a3::8a2e:0370:7334
  • 2001:db8:85a3:0:0:8a2e:370
  • 127.0.0
  • example.com
  • 192.168.1.1:80

Common pitfalls

  • Does not match compressed IPv6 addresses (e.g., with ::)
  • Does not validate IP ranges (e.g., 256.0.0.1 passes digit check but is invalid)
  • Does not account for leading/trailing whitespace (anchors ^ and $ prevent this)
  • Does not handle mixed-case IPv6 addresses beyond the character class (e.g., '2001:0DB8...' is valid due to case-insensitive class)

Performance notes

The pattern is linear in complexity for both IPv4 and IPv6 branches, with no nested quantifiers that could cause catastrophic backtracking. The alternation is simple and does not introduce significant overhead. Ensure input is trimmed if whitespace is a concern, as ^ and $ anchors will not match otherwise.

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 regex match compressed IPv6 addresses like '2001:db8::1'?

No, the pattern requires exactly 8 segments separated by colons, so compressed forms with :: are not matched.

Why does the IPv4 part use \d{1,3} instead of stricter validation?

The pattern checks for 1-3 digits per octet but does not validate the numeric range (0-255). Stricter validation would require additional logic (e.g., negative lookaheads or separate digit checks).

Can this regex be used to validate IP addresses in user input?

Not reliably for production use. It matches syntactically valid IPs but does not enforce semantic rules (e.g., valid IP ranges, compressed IPv6, or CIDR notation). Consider using a dedicated IP validation library.