http://tools.ietf.org/html/rfc4291#section-2.2 olarak ve http://tools.ietf.org/html/rfc5952#section-4.2 sadece bu "0000" dışarı sıyırma açıklar.
Sadece bir grup siper için "::" ikamesi yapılmamalıdır, sadece adreste bir kez yapılabilir ve her bir basamak grubunun içinde 0'lık sıyırma sıyırma işlemi gerçekleştirilmelidir. Ayrıca IPv4 haritalaması da dikkate alınmalıdır.
Kullanayım:
function ip_notation(ip){
var ip=ip.toLowerCase();
//Watch out IPv6/IPv4 addresses notation
//::ffff:XXX.XXX.XXX.XXX vs ::ffff:xxxx:xxxx
if(ip.substr(0,24)=='00000000000000000000ffff'){
ip4= (ip.substr(24));
ip4=ip4.match(/.{1,2}/g);
for(k=0;k<4;k++){ip4[k]=parseInt(ip4[k],16);}
ip='::ffff:'+ ip4.join('.');
return ip;
}
field=ip.match(/.{1,4}/g);//Cut string in 4 digits fields
//Find the longest ceroes fields group (maybe could be done with a regex)
var max_ceroes_fields=0;
var ceroes_fields=0;
for(k=0;k<8;k++){
if(field[k] == '0000') { //All '0' field
ceroes_fields++;
if(ceroes_fields > max_ceroes_fields) {
max_ceroes_fields = ceroes_fields;
}
}else{//Not all '0' field
ceroes_fields = 0;
}
}
ip=field.join(":");//makes a string again, now with 4 digit groups
//replace the longest ceroes group with "::"
if(max_ceroes_fields>1) {
var ceroes=(":0000".repeat(max_ceroes_fields)).substr(1);
ip=ip.replace(ceroes,':');
//Works fine if it is at the start or end, but produces ":::" in the middle
ip=ip.replace(':::','::');
}
//Strip leading ceroes of fields
ip.replace(/^(0){1,3}/,'');
return ip;
}
olamazlar [ ' "% ip%" .Kapağı (/^(0000:) + /)'] (https://regex101.com/r/jU7qM5/1)? Yoksa bir şey mi özlüyorum? – Yoshi
@Yoshi, lütfen bunu yanıt olarak kabul edip çözümünüzü kabul edebilirim .. –
https://tools.ietf.org/html/rfc4291#section-2.2 ve https://tools.ietf.org adresindeki IPv6 gösterimini kontrol edin /html/rfc5952#section-4.2 Dikkatli olun, sadece dize başlangıcında "0000:" sıfıra basmak geçerli bir adres veremiyor (çift başlatma eksikliği: ") –