An IP address is the label that identifies a machine on a network, and it comes in two formats you have to tell apart: IPv4, four decimal numbers joined by dots like 192.168.1.1, and IPv6, up to eight groups of hexadecimal digits joined by colons like 2001:db8::1. You will build a small validator, ipAddressValidate = { isIPv4, isIPv6, version }, that decides whether a string is a well-formed address of either kind. The catch is in the details: an IPv4 octet runs 0–255 with no leading zeros, and IPv6 lets a run of all-zero groups collapse into a single ::. See IPv4 and IPv6 on MDN for background.
ipAddressValidate.isIPv4(str) // string -> boolean
ipAddressValidate.isIPv6(str) // string -> boolean
ipAddressValidate.version(str) // string -> 4 | 6 | 0
version returns 4 for a valid IPv4 address, 6 for a valid IPv6 address, and 0 for anything else.
ipAddressValidate.isIPv4('192.168.1.1'); // true
ipAddressValidate.isIPv4('256.0.0.1'); // false — 256 is out of range
ipAddressValidate.isIPv4('01.2.3.4'); // false — leading zero
ipAddressValidate.isIPv6('::1'); // true
ipAddressValidate.isIPv6('2001:db8::8a2e:370:7334'); // true
ipAddressValidate.isIPv6('1:2:3:4:5:6:7:8:9'); // false — nine groups
ipAddressValidate.version('10.0.0.1'); // 4
ipAddressValidate.version('fe80::'); // 6
ipAddressValidate.version('nope'); // 0
., each all digits, valued 0 to 255, with no leading zero: 0 is fine, 01 is not.0–9, a–f, case-insensitive) split on :. Leading zeros inside a group are allowed, so 0db8 is fine.:: shorthand — one :: may replace a run of one or more all-zero groups, and it may appear at most once. :: alone is the all-zeros address; ::1 and fe80:: are valid.Number('1e2') is 100 and Number('') is 0, so numeric coercion cannot validate an octet. Check the digits yourself./24, no port numbers, and no IPv4-mapped IPv6 like ::ffff:192.0.2.1. Assume the input is a bare address string.