-1

I need a regular expression that only validates an IPv6 address in Javascript. I have tried the following two which both fail on a string like 1:1:1:1:1:1:1:1

^([\dA-F]{1,4}:|((?=.*(::))(?!.*\3.+\3))\3?)([\dA-F]{1,4}(\3|:\b)|\2){5}(([\dA-F]{1,4}(\3|:\b|$)|\2){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z

^([0-9A-Fa-f]{0,4}:){2,7}([0-9A-Fa-f]{1,4}$|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})$
JoGotta
  • 215
  • 2
  • 13

1 Answers1

-2

here you go

[0-9a-fA-F].{0,3}+:[0-9a-fA-F].{0,3}+:[0-9a-fA-F].{0,3}+:[0-9a-fA-F].{0,3}+:[0-9a-fA-F].{0,3}+:[0-9a-fA-F].{0,3}+:[0-9a-fA-F].{0,3}+:[0-9a-fA-F].{0,3}

oops, forgot to set it to a max of 4 per segment.

Note that there are spcific combinations for ip address where some are not valid. But this will fit your requirment.

Joseph Dailey
  • 4,735
  • 2
  • 15
  • 18
  • 1
    -1 : your regex fails to accept `::1` which is the IPv6 loopback address. – xxbbcc May 28 '14 at 20:11
  • Are you sure that you want the dots between the `[...]` and the `{...}`? – Bill Lynch May 28 '14 at 20:14
  • Fixed issue from sharth's comment, thanks. I don't know regular expression enough to fix the issue noted in xxbbcc''ss comment – Joseph Dailey May 28 '14 at 20:15
  • @JosephDailey IPv6 addresses are fairly complex. I wouldn't use a regex to check them but a proper parser. See http://en.wikipedia.org/wiki/IPv6_address – xxbbcc May 28 '14 at 20:16
  • I know how ipv6 works to an extent but when validating that somthing is 64bit hex values separated by colons, this would suffice. – Joseph Dailey May 28 '14 at 20:47