js版IPv6的正则表达式


JavaScript #ipv6 #正则表达式 #函数2012-07-10 10:14
大家都知道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写了一个迂回的判断方法,如下,有问题,请指教:

// http://yige.org 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;  
}  

相关文章

粤ICP备11097351号-1