468. 验证 IP 地址
解法一:依次遍历
/**
* @param {string} queryIP
* @return {string}
*/
var validIPAddress = function (queryIP) {
const ipv4List = queryIP.split(".");
if (ipv4List.length === 4) {
for (let i of ipv4List) {
if (String(i * 1) !== i || Number(i) > 255) {
return "Neither";
}
}
return "IPv4";
}
const ipv6List = queryIP.split(":");
if (ipv6List.length === 8) {
for (let i of ipv6List) {
const reg = /^[0-9a-fA-F]{1,4}$/;
if (!reg.test(i) || isNaN(parseInt(i, 16))) {
return "Neither";
}
}
return "IPv6";
}
return "Neither";
};