4 ms·
Because empty octets of an IPv6 address can be elided (1:0:0:0:0:0:0:2 can be represented as 1::2) the regular expressions are actually quite painful. I wrote a
by beaugunderson 3y ago
Because empty octets of an IPv6 address can be elided (1:0:0:0:0:0:0:2 can be represented as 1::2) the regular expressions are actually quite painful. I wrote a library for JavaScript called `ip-address` that can generate regular expressions to find all forms of a specific address. Here's the regular expression for all forms of 1::2:
/(?=^|[^A-Fa-f0-9:]|[^\w\:])((0{0,3}1):(|(:0{1,4}){1,5}|(0{1,4}:){1,5}|(0{1,4}:){5}0{1,4}|(0{1,4}:){1}:(0{1,4}:){3}0{1,4}|(0{1,4}:){2}:(0{1,4}:){2}0{1,4}|(0{1,4}:){3}:(0{1,4}:){1}0{1,4}|(0{1,4}:){4}:(0{1,4}:){0}0{1,4}|(0{1,4}:){1}:(0{1,4}:){2}0{1,4}|(0{1,4}:){2}:(0{1,4}:){1}0{1,4}|(0{1,4}:){3}:(0{1,4}:){0}0{1,4}|(0{1,4}:){1}:(0{1,4}:){1}0{1,4}|(0{1,4}:){2}:(0{1,4}:){0}0{1,4}|(0{1,4}:){1}:(0{1,4}:){0}0{1,4}):(0{0,3}2))(?=[^\w\:]|[^A-Fa-f0-9:]|$)/i