js版IPv6的正则表达式


JavaScript #ipv6 #正则表达式 #函数2012-07-10 10:14
大家都知道IPv4的正则表达式很好写,如下:

1^((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格式的简单介绍:
1Format is x:x:x:x:x:x:x:x 
2x is a 16 bit hexadecimal field 
3FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 
4Leading zeros in a field are optional 
5:: can be used to represent multiple groups of 16 bits of zero 
6:: can only be used once in an address 
7FF01:0:0:0:0:0:0:101 = FF01::101 
80:0:0:0:0:0:0:1 = ::1 
90:0:0:0:0:0:0:0 = ::
 
不知道谁可以给出一个验证IPv6的正则表达式啊?或者,用迂回的方法也行,即可以通过代码进行判断。

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

1^\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写了一个迂回的判断方法,如下,有问题,请指教:

01// http://yige.org check whether the str is a right IPv6 address 
02function checkIPv6(str) { 
03    var idx = str.indexOf("::"); 
04    // there is no "::" in the ip address 
05    if (idx == -1) { 
06        var items = str.split(":"); 
07        if (items.length != 8) { 
08            return false
09        } else
10            for (i in items) { 
11                if (!isHex(items[i])) { 
12                    return false
13                
14            
15            return true
16        
17    } else
18        // at least, there are two "::" in the ip address 
19        if (idx != str.lastIndexOf("::")) { 
20            return false
21        } else
22            var items = str.split("::"); 
23            var items0 = items[0].split(":"); 
24            var items1 = items[1].split(":"); 
25            if ((items0.length + items1.length) > 7) { 
26                return false
27            } else
28                for (i in items0) { 
29                    if (!isHex(items0[i])) { 
30                        return false
31                    
32                
33                for (i in items1) { 
34                    if (!isHex(items1[i])) { 
35                        return false
36                    
37                
38                return true
39            
40        
41    
42
43   
44// check whether every char of the str is a Hex char(0~9,a~f,A~F) 
45function isHex(str) { 
46    if(str.length == 0 || str.length > 4) { 
47        return false
48    
49    str = str.toLowerCase(); 
50    var ch; 
51    for(var i=0; i< str.length; i++) { 
52        ch = str.charAt(i); 
53        if(!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'f')) { 
54            return false
55        
56    
57    return true
58}

相关文章

粤ICP备11097351号-1