简体   繁体   English

如何使用 javascript 压缩 IPV6 地址?

[英]How to compress IPV6 address using javascript?

I have seen the code to compress IPV6 in java. The link specifies the same.我在java看到了压缩IPV6的代码。 链接指定相同。 Below is the code in Java. String resultString = subjectString.replaceAll("((?::0\\b){2,}):?(?:\\S*\\b\\1,0\\b)(\\S*)": ":;$2");下面是Java中的代码。 String resultString = subjectString.replaceAll("((?::0\\b){2,}):?(?:\\S*\\b\\1,0\\b)(\\S*)": ":;$2"); But in Javascript I am confused as how can I get the regex expression to match the same.但是在 Javascript 中,我很困惑如何才能让正则表达式匹配相同的内容。 Can you share some pointers here?你能在这里分享一些建议吗?

Example: fe80:00:00:00:8e3:a11a:2a49:1148 Result: fe80::8e3:a11a:2a49:1148示例:fe80:00:00:00:8e3:a11a:2a49:1148 结果:fe80::8e3:a11a:2a49:1148

There's a couple problems with the other answer by @ClasG: @ClasG 的另一个答案有几个问题:

  1. If the repeating zeroes are at the beginning of the IPv6 address or it's all zeroes, only 1 colon is replaced.如果重复的零位于 IPv6 地址的开头或全部为零,则仅替换 1 个冒号。
  2. If the repeating zeroes are at the end, they're not replaced.如果重复的零在末尾,则不会替换它们。

I suggest using the regex \\b:?(?:0+:?){2,} and have it replaced with :: (two colons)我建议使用正则表达式\\b:?(?:0+:?){2,}并将其替换为:: :(两个冒号)

Regex101 tests Regex101 测试

JavaScript example: JavaScript 示例:

 var ips = [ '2001:0db8:ac10:0000:0000:0000:0000:ffff', '2001:0db8:ac10:0000:0000:0000:0000:0000', '0:0:0:0:0:2001:0db8:ac10', '2001:0db8:ac10:aaaa:0000:bbbb:cccc:ffff', '2001:0db8:ac10:0000:0000:bbbb:00:00' ]; for (var i = 0; i < ips.length; i++) { document.write(ips[i].replace(/\\b:?(?:0+:?){2,}/, '::') + "<br>"); }

Note: The Regex101 tests replace multiple repeating groups of zeroes.注意:Regex101 测试替换了多个重复的零组。 In XYZ programming language, you'll have to limit the number of replacements to 1. In JavaScript, you omit the g lobal flag.在XYZ的编程语言,你就必须更换的次数限制为1。在JavaScript中,省略了叶形标志。 In PHP, you set the $limit for preg_replace to 1.在 PHP 中,您将preg_replace$limit设置为 1。

You can do it by replacing你可以通过替换来做到

\b(?:0+:){2,}

with

:

 function compIPV6(input) { return input.replace(/\\b(?:0+:){2,}/, ':'); } document.write(compIPV6('2001:db8:0:0:0:0:2:1') + '<br/>'); document.write(compIPV6('fe80:00:00:00:8e3:a11a:2a49:1148' + '<br/>'));

Check it out at regex101 .在 regex101 上查看

You can use this method in order to compress IPv6 AND remove leading 0s:您可以使用此方法来压缩 IPv6 并删除前导 0:

 function compressIPV6(input) { var formatted = input.replace(/\\b(?:0+:){2,}/, ':'); var finalAddress = formatted.split(':') .map(function(octet) { return octet.replace(/\\b0+/g, ''); }).join(':'); return finalAddress; } document.write(compressIPV6('2001:0db8:0000:0000:0000:0000:1428:57ab') );

You can use a function that considers all of the needed cases:您可以使用 function 来考虑所有需要的情况:

 const compressIPV6 = (ip) => { //First remove the leading 0s of the octets. If it's '0000', replace with '0' let output = ip.split(':').map(terms => terms.replace(/\b0+/g, '') || '0').join(":"); //Then search for all occurrences of continuous '0' octets let zeros = [...output.matchAll(/\b:?(?:0+:?){2,}/g)]; //If there are occurences, see which is the longest one and replace it with '::' if (zeros.length > 0) { let max = ''; zeros.forEach(item => { if (item[0].replaceAll(':', '').length > max.replaceAll(':', '').length) { max = item[0]; } }) output = output.replace(max, '::'); } return output; } document.write(compressIPV6('38c1:3db8:0000:0000:0000:0000:0043:000a') + '<br/>'); document.write(compressIPV6('0000:0000:0000:0000:38c1:3db8:0043:000a') + '<br/>'); document.write(compressIPV6('38c1:3db8:0000:0043:000a:0000:0000:0000') + '<br/>'); document.write(compressIPV6('38c1:0000:0000:3db8:0000:0000:0000:12ab') + '<br/>');

If there's more than one occurrence of consecutive '0' octets of the same length, it will only replace the first one.如果多次出现相同长度的连续“0”八位字节,它只会替换第一个。 This will work regardless if the repeating zeroes are at the beginning, at the middle or at the end.无论重复零是在开头、中间还是结尾,这都将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM