Regex to Match an IPv6 Address
Copy the pattern, paste your text below, and watch it highlight every full-form IPv6 address. Below the tester: what each group means and why :: compression needs more.
Regex pattern
/(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}/g
This matches a full or compressed IPv6 address, including :: zero-compression and IPv4-mapped forms like ::ffff:192.0.2.1. It validates the textual format, not that the address is routable.
What this pattern matches
This matches a full, uncompressed IPv6 address: eight groups of one to four hexadecimal digits separated by colons.
How it works, part by part
[A-Fa-f0-9]{1,4} — one hextet: one to four hex digits.
(?:…:){7} — that hextet followed by a colon, repeated seven times.
[A-Fa-f0-9]{1,4} — the final eighth hextet.
About :: compression
IPv6 lets you collapse runs of zero groups to :: (e.g. 2001:db8::1). Matching every compressed form needs a much longer alternation; use this pattern when your data is normalised to full form.
Use it in your code
import re
text = "Full: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 and fe80:0000:0000:0000:0202:b3ff:fe1e:8329. Compressed 2001:db8::1 is not matched by this simple form."
pattern = r'''(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Full: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 and fe80:0000:0000:0000:0202:b3ff:fe1e:8329. Compressed 2001:db8::1 is not matched by this simple form.";
const re = new RegExp(String.raw`(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}`, "g");
console.log(text.match(re));
FAQ
Does it match compressed addresses like 2001:db8::1?
No. This simple pattern matches only the full eight-group form. Compressed :: forms need a longer alternation.
Is it case sensitive?
No. It accepts both upper and lower hex digits (A–F and a–f).