var ipStr =
/^(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))$/;
// ipv6正则参考:https://github.com/kriasoft/cloudflare-ips/blob/91a8be0a53fd8e1baf2a532102bd243e9cb5cbec/fetch.test.js
var ipStr6 =
/^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/
function validIPv46(IP) {
if(ipStr6.test(IP)||ipStr.test(IP)){
return true
};
return false
};
function validIP(IP) {
return validIPv46(IP); //验证ipv4或ipv6(将4和6的正则合成一个正则一直有问题,使用函数封装正常)
//return ipStr.test(IP); //验证ipv4
//return ipStr6.test(IP); //验证ipv6
};
console.log("------正常------")
console.log(validIP('2001:0db8:85a3:0:0:8A2E:0370:7334'));
console.log(validIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
console.log(validIP('2001:db8:85a3:0:0:8A2E:0370:7334'));
console.log(validIP('f:f:f:f:f:f:f:f'));
console.log(validIP('1050:0000:0000:0000:0005:0600:300c:326b'));
console.log(validIP('1050:0:0:0:5:600:300c:326b'));
console.log(validIP('2001:0db8:85a3::8a2e:0370:7334'));
console.log(validIP('1:2:3:4:5:6:7:8'))
console.log(validIP('1::8'))
console.log(validIP('1::7:8'))
console.log(validIP('1::6:7:8'))
console.log(validIP('1::5:6:7:8'))
console.log(validIP('1::4:5:6:7:8'))
console.log(validIP('1::3:4:5:6:7:8'))
console.log(validIP('::2:3:4:5:6:7:8'))
console.log(validIP('1:2:3:4:5:6::8'))
console.log(validIP('1:2:3:4:5::7:8'))
console.log(validIP('1:2:3:4::6:7:8'))
console.log(validIP('1:2:3::5:6:7:8'))
console.log(validIP('1:2::4:5:6:7:8'))
console.log(validIP('1::3:4:5:6:7:8'))
console.log(validIP('::2:3:4:5:6:7:8'))
console.log(validIP('1:2:3:4:5:6:7::'))
console.log(validIP('1:2:3:4:5:6::8'))
console.log(validIP('1:2:3:4:5::8'))
console.log(validIP('1:2:3:4::8'))
console.log(validIP('1:2:3::8'))
console.log(validIP('1:2::8'))
console.log(validIP('1::8'))
console.log("------特殊情况------")
console.log(validIP('1::'))
console.log(validIP('::8'))
console.log(validIP('::'))
console.log(validIP('2001:db8:3:4::192.0.2.33'));//修改ip时不支持兼容模式
console.log("------异常------")
console.log(validIP('172.16.254.1'));
console.log(validIP('2001:0db8:85a3::8a2e:0370:7334:8a2e:0370:7334'));
console.log(validIP('256.256.256.256'));
console.log(validIP('172.16.254.01'));
console.log(validIP('2001:0db8:85a3:::8A2E:0370:7334'));
console.log(validIP('02001:0db8:85a3:0000:0000:8a2e:0370:7334'));
console.log(validIP('fd30::20::11'))
console.log('-----------ipv4--------')
console.log(validIP('172.16.254.1'));
console.log(validIP('1.1.1.1'));
console.log(validIP('255.255.255.255'));
console.log(validIP('0.0.0.0'));
console.log(validIP('1.0.0.1'));
网友评论