JavaScript-compatible regex reference

IPv6 Address Regex Pattern & Tester

Test common full and compressed IPv6 address forms, including the double-colon abbreviation used to collapse consecutive zero groups. IPv6 has substantially more valid textual forms than IPv4, so this page also explains when a platform IP parser is a better production choice.

Pattern

^(?:(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,7}:|(?:[0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}|(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}|(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}|(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:(?:(?::[0-9a-f]{1,4}){1,6})|:(?:(?::[0-9a-f]{1,4}){1,7}|:))$

Flags: i

Live test vectors

Test the IPv6 address regex

Enter one value per line. Each line is tested against the whole-string pattern using the browser JavaScript RegExp engine.

PASS2001:db8:85a3:0:0:8a2e:370:7334
PASS2001:db8::1
PASS::1
PASS::
FAIL2001:db8:::1
FAIL2001:db8:zzzz::1
FAIL2001:db8:1:2:3:4:5:6:7
FAIL2001:db8

Quick answer

IPv6 validation requires accounting for both eight-group full notation and multiple legal placements of the :: compression marker. A short “hex groups separated by colons” regex usually rejects valid compressed addresses or accepts too many groups.

Common use cases

  • Network configuration validation
  • Infrastructure inventory checks
  • Log-field prevalidation

Pitfalls and limitations

  • !IPv6 compression makes a complete regex much longer than an IPv4 pattern.
  • !This pattern focuses on hexadecimal IPv6 forms and does not attempt every IPv4-embedded or zone-identifier representation.
  • !For security-sensitive network software, prefer the runtime or platform IP-address parser because it can normalize and classify addresses after parsing.

Known-valid examples

2001:db8:85a3:0:0:8a2e:370:73342001:db8::1::1::

Known-invalid examples

2001:db8:::12001:db8:zzzz::12001:db8:1:2:3:4:5:6:72001:db8

Continue testing and debugging

Frequently asked questions

Why is an IPv6 regex so long?

IPv6 allows eight hexadecimal groups plus a double-colon abbreviation that can replace one or more consecutive zero groups in several positions. Enumerating those valid shapes makes the expression longer.

Does this pattern support ::1 and ::?

Yes. Loopback shorthand and the all-zero unspecified-address shorthand are included.

Should production code use regex for IPv6?

Regex can be useful for front-end checks and data screening, but a purpose-built IP parser is preferable when you need normalization, address-family handling, reserved-range classification, zone identifiers, or IPv4-embedded forms.