ipv6合法性校验

该代码实现了一个JavaScript函数validateIpv6,用于验证输入的字符串是否为有效的IPv6地址。它检查地址是否符合格式要求,包括双冒号的使用、分段数量以及每个段的数值范围。函数通过拆分、过滤和正则匹配来完成验证。
摘要由CSDN通过智能技术生成

export function validateIpv6 (a) {
const ns = []
const nh = a.split(‘::’)
if (nh.length > 2) {
return false
} else if (nh.length === 2) {
if (nh[0].startsWith(‘:’) || nh[0].endsWith(‘:’) || nh[1].startsWith(‘:’) || nh[1].endsWith(‘:’)) {
return false
}
ns.push(…(nh[0].split(‘:’).filter(a => a)))
ns.push(…(nh[1].split(‘:’).filter(a => a)))
if (ns.length > 7) {
return false
}
} else if (nh.length === 1) {
ns.push(…(nh[0].split(‘:’).filter(a => a)))
if (ns.length !== 8) {
return false
}
}

for (const n of ns) {
const match = n.match(/1{1,4}$/i)
if (!(match !== undefined && match !== null) || (match[0] && (match[0] !== n))) {
return false
}
}
return true
}


  1. a-f0-9 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值