IPv6的正则表达式【JAVASCRIPT】

大家都知道IPv4的正则表达式很好写,如下:

^((25[0-5]|2[0-4]\d|[0-1]?\d{1,2})\.){3}(25[0-5]|2[0-4]\d|[0-1]?\d{1,2})$

 但是IPv6的格式相比较而言,就复杂了不止100倍啊,下面是IPv6格式的简单介绍:

Format is x:x:x:x:x:x:x:x
x is a 16 bit hexadecimal field
FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
Leading zeros in a field are optional
:: can be used to represent multiple groups of 16 bits of zero
:: can only be used once in an address
FF01:0:0:0:0:0:0:101 = FF01::101
0:0:0:0:0:0:0:1 = ::1
0:0:0:0:0:0:0:0 = ::

 

不知道谁可以给出一个验证IPv6的正则表达式啊?或者,用迂回的方法也行,即可以通过代码进行判断。

在网上找到一个IPv6的表达式,如下:

 

^\s*((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?\s*$

 

但是这么复杂得无法进行判断啊。

 

 

我自己用JAVASCRIPT写了一个迂回的判断方法,如下,有问题,请指教:

// check whether the str is a right IPv6 address
function checkIPv6(str) {
	var idx = str.indexOf("::");
	// there is no "::" in the ip address
	if (idx == -1) {
		var items = str.split(":");
		if (items.length != 8) {
			return false;
		} else {
			for (i in items) {
				if (!isHex(items[i])) {
					return false;
				}
			}
			return true;
		}
	} else {
		// at least, there are two "::" in the ip address
		if (idx != str.lastIndexOf("::")) {
			return false;
		} else {
			var items = str.split("::");
			var items0 = items[0].split(":");
			var items1 = items[1].split(":");
			if ((items0.length + items1.length) > 7) {
				return false;
			} else {
				for (i in items0) {
					if (!isHex(items0[i])) {
						return false;
					}
				}
				for (i in items1) {
					if (!isHex(items1[i])) {
						return false;
					}
				}
				return true;
			}
		}
	}
}

// check whether every char of the str is a Hex char(0~9,a~f,A~F)
function isHex(str) {
	if(str.length == 0 || str.length > 4) {
		return false;
	}
	str = str.toLowerCase();
	var ch;
	for(var i=0; i< str.length; i++) {
		ch = str.charAt(i);
		if(!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'f')) {
			return false;
		}
	}
	return true;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值