Regex to Match IPv6 Addresses
Match full-form IPv6 addresses with this regex pattern. Validates all eight groups of one to four hexadecimal digits separated by colons. Free.
Regular Expression
/(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}/g
Token Breakdown
| Token | Description |
|---|---|
| (?: | Start of non-capturing group |
| [0-9a-fA-F] | Character class — matches any one of: 0-9a-fA-F |
| {1,4} | Matches between 1 and 4 times |
| : | Matches the literal character ':' |
| ) | End of group |
| {7} | Matches exactly 7 times |
| [0-9a-fA-F] | Character class — matches any one of: 0-9a-fA-F |
| {1,4} | Matches between 1 and 4 times |
Detailed Explanation
This regex matches full (non-abbreviated) IPv6 addresses consisting of eight groups of hexadecimal digits. Here is the token-by-token breakdown:
(?: — Opens a non-capturing group for the first seven groups, each followed by a colon.
[0-9a-fA-F]{1,4} — A character class matching hexadecimal digits. It includes digits 0-9, lowercase letters a-f, and uppercase letters A-F. The {1,4} quantifier requires between one and four hex digits per group. IPv6 groups can have leading zeros omitted, so a single digit like '0' is valid, as is a full group like 'fe80'.
: — Matches a literal colon separating the groups.
){7} — Closes the non-capturing group and repeats it exactly seven times, covering the first seven groups and their trailing colons.
[0-9a-fA-F]{1,4} — Matches the eighth and final group of one to four hexadecimal digits, without a trailing colon.
The g flag enables global matching to find all IPv6 addresses in the text. Note that this pattern matches the full, expanded form of IPv6 addresses only. It does not handle the abbreviated forms with :: (double colon) notation, which is used to compress consecutive groups of zeros. For most validation purposes where addresses are stored in their full form, this pattern works well. For abbreviated forms, a more complex pattern or multiple alternatives would be needed.
Example Test Strings
| Input | Expected |
|---|---|
| 2001:0db8:85a3:0000:0000:8a2e:0370:7334 | Match |
| fe80:0000:0000:0000:0000:0000:0000:0001 | Match |
| ::1 | No Match |
| 2001:db8::1 | No Match |
| 0000:0000:0000:0000:0000:0000:0000:0000 | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
40 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match IPv4 Addresses
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g
Regex to Match CIDR Notation
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/(?:3[0-2]|[12]?\d)\b/g
Regex to Match MAC Addresses
/([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}/gi
Regex to Match Hexadecimal Numbers
/^(?:0x)?[0-9a-fA-F]+$/i