根据长度(<4)提取特殊字符之间的子字符串,并将缺失的字符替换为0,使其为4
Extract substring between special character based on its length(<4) and replace the missing characters with 0 to make it 4
我正在尝试开发一个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,我使用了显式变量名,所以很容易阅读。
相关文章:
- 无效字符替换时出错
- 在JavaScript中,如何用一个字符替换字符串中的所有字母
- 不能将 UTF-8 字符替换为正则表达式
- .keyup() 上的非拉丁字符替换在快速键入时不起作用
- 将字符串中的重复字符替换为不断增长的数字 [Javascript]
- 为什么不用特殊字符替换选择文本,以及如何删除此特殊字符
- 将ASCII字符替换为等效字符
- Javascript:用其他字符替换反斜杠和双引号
- Angularjs:显示字符串中的最后四个字符,并将其余字符替换为“X”
- 用字符替换将字符串之类的数组替换为数组
- 用连字符替换字符串中的空格
- 特殊字符替换为空格
- 将所有字符替换为 #,最后 4 个字符除外
- 将文本上的特殊字符替换为 jquery
- 将字符替换为标记(例如 *hi* 到
hi
) - JS中的多个字符替换 - 如何在同一函数中停止对先前结果的替换
- 删除时将字符替换为另一个字符
- 将所有特定字符替换为计数器升序
- Javascript 将 32 以下的字符替换为 x(charcode)
- Javascript:正则表达式,用于将某些字符替换为十六进制代码