根据长度(<4)提取特殊字符之间的子字符串,并将缺失的字符替换为0,使其为4

Extract substring between special character based on its length(<4) and replace the missing characters with 0 to make it 4

本文关键字:字符 替换 字符串 lt 提取 之间 特殊字符      更新时间:2023-09-26

我正在尝试开发一个IPv6子网计算器,我需要以其扩展格式显示地址。也就是说,假设我们有这个IP地址:2001:DB8:C003:1::F00D/48,我需要根据已经存在的字段数(最多8个字段)将::替换为零,如:0000:0000:0000:
我用switch语句完成了这一操作,但IPv6地址在其浓缩符号中也省略了前导零。根据上述IP地址,:DB8被浓缩。它实际上是:0DB8,我也需要适应它。

这就是我迄今为止所尝试的:

代码

 /*To calculate hex field*/
var field = (ip.split(":").length - 1);
var condens = ":0000:0000:0000:0000:0000:0000:0000";
switch(field)
{
    case 1:
    {
        var block=ip.substring(ip.lastIndexOf(":")+1,ip.lastIndexOf(":")); //to extract strings between the special character
        var inputString = ip,
                expand = inputString.replace(/(::)+/g, condens);
        console.log(expand);
        while(block.length < 4)
        {
            //replace fields only with missing characters with 0
        }
        $("#expand").attr("value",expand);
        break;
    }
    case 2:
    {
        var inputString = ip,
                expand = inputString.replace(/(::)+/g, ":0000:0000:0000:0000:0000:0000:0000");
        console.log(expand);
        $("#expand").attr("value",expand);
        break;
    }
    case 3:
    {
        var inputString = ip,
                expand = inputString.replace(/(::)+/g, ":0000:0000:0000:0000:0000:");
        console.log(expand);
        $("#expand").attr("value",expand);
        break;
    }
    case 4:
    {
        var inputString = ip,
                expand = inputString.replace(/(::)+/g, ":0000:0000:0000:0000:");
        console.log(expand);
        $("#expand").attr("value",expand);
        break;
    }
    case 5:
    {
        var inputString = ip,
                expand = inputString.replace(/(::)+/g, ":0000:0000:0000:");
        console.log(expand);
        $("#expand").attr("value",expand);
        break;
    }
    case 6:
    {
        var inputString = ip,
                expand = inputString.replace(/(::)+/g, ":0000:0000:");
        console.log(expand);
        $("#expand").attr("value",expand);
        break;
    }
}


如您所见,当前我正在提取:之间的子字符串。我只需要提取长度<4,并将前导零相加,使长度达到4。然后,将我提取的子串替换为新的子串(带有前导零并打印它。

有人能帮帮我吗?谢谢

它可以用一种更简单的方法来完成:

var input = '2001:DB8:C003:1::F00D/48';
var tmp = input.split(/'//);
var ip = tmp[0];
var subnet = tmp[1];
// if the ip is compacted
if (ip.match('::')) {
    // + 1 back because there is an extra empty element in the array (because of ::)
    var missingPartsNumber = 8 - ip.split(':').length + 1;
    // replace the :: by the number of : needed to have 8 parts in the ip
    ip = ip.replace(/::/, pad('', missingPartsNumber + 1, ':'));
}
var paddedIp = ip.split(':').map(function (part) {
    return pad(part, 4, '0')
}).join(':') + '/' + subnet; // add the subnet back if needed
// from http://stackoverflow.com/a/10073788/5388620
function pad(n, width, z) {
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
snippet.log('in : ' + input);
snippet.log('out: ' + paddedIp);
<script src="https://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

更新

更新了我的代码以实际返回一个有效的IPV6,我使用了显式变量名,所以很容易阅读。