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

TokenDescription
(?: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

InputExpected
2001:0db8:85a3:0000:0000:8a2e:0370:7334Match
fe80:0000:0000:0000:0000:0000:0000:0001Match
::1No Match
2001:db8::1No Match
0000:0000:0000:0000:0000:0000:0000:0000Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

2001:0db8:85a3:0000:0000:8a2e:0370:7334 fe80:0000:0000:0000:0000:0000:0000:0001 ::1 2001:db8::1 0000:0000:0000:0000:0000:0000:0000:0000

Matches & Capture Groups

#12001:0db8:85a3:0000:0000:8a2e:0370:7334index 0
#2fe80:0000:0000:0000:0000:0000:0000:0001index 40
#30000:0000:0000:0000:0000:0000:0000:0000index 96
Pattern: 40 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →