regex 使用Javascript编辑IPv4和IPv6地址

k10s72fa  于 2023-02-05  发布在  Java
关注(0)|答案(1)|浏览(199)

概述

我正在构建一个包含两个文本区域inputoutput的网站
这个概念相当简单,我想:

  • 使用regex解析输入以匹配任何IPv4/IPv6地址
  • 使用. replace或任何其他方法将它们修订为[IPv4]、[IPv6
  • output显示输出

我有它的工作完美的ipv4,我可以粘贴在一个巨大的文本块在和它编校所有ipv4地址的预期。
∮问题是
正则表达式,或者可能是javascript replace函数无法正常工作。

重新创建问题的HTML

<html>
<head>
</head>
   <body>
   <div>
      <label for="input" class= "left">Input Text</label>
      <label for="output" class = "right">Obfuscated Text - Click Text to Copy</label>
      <span></span>
      <textarea id="input"></textarea>
      <textarea id="output"></textarea>
   </div>
   <br>
   <button onclick="obfuscate()">Obfuscate</button>
   <script>
      function obfuscate() {
         // Get the input and output textareas
         var input = document.getElementById("input");
         var output = document.getElementById("output");

         // Obfuscate the text in the input textarea
         var obfuscatedText = input.value.replace(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g, "[IPv4]")
         var lines = obfuscatedText.split('\n')
         const modifiedLines = lines.map(line => line.replace(/\b(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\b/,"[IPv6]"));

         // Set the value of the output textarea to the obfuscated text
         output.value = modifiedLines.join('\n');
      }
   </script>
   </body>
</html>

注解

最初在[IPv6]中没有任何修改,我在网上看到replace不能处理很长的字符串。所以我把它分解成单独的行。这样做效果更好,但仍然不能替换所有的匹配。

当前行为

即使input很小

subnet6 3ffe:501:ffff:100::/64 {
    # Two addresses available to clients
    #  (the third client should get NoAddrsAvail)
    range6 3ffe:501:ffff:100::10 3ffe:501:ffff:100::11;

    # Use the whole /64 prefix for temporary addresses
    #  (i.e., direct application of RFC 4941)
    range6 3ffe:501:ffff:100:: temporary;

    # Some /64 prefixes available for Prefix Delegation (RFC 3633)
    prefix6 3ffe:501:ffff:100:: 3ffe:501:ffff:111:: /64;
}

# A second subnet behind a relay agent
subnet6 3ffe:501:ffff:101::/64 {
    range6 3ffe:501:ffff:101::10 3ffe:501:ffff:101::11;

    # Override of the global definitions,
    # works only when a resource (address or prefix) is assigned
    option dhcp6.name-servers 3ffe:501:ffff:101:200:ff:fe00:3f3e;

}

它无法编辑所有v6地址
输出:

subnet6 3ffe:501:ffff:100::/64 {
    # Two addresses available to clients
    #  (the third client should get NoAddrsAvail)
    range6 [IPv6]10 3ffe:501:ffff:100::11;

    # Use the whole /64 prefix for temporary addresses
    #  (i.e., direct application of RFC 4941)
    range6 3ffe:501:ffff:100:: temporary;

    # Some /64 prefixes available for Prefix Delegation (RFC 3633)
    prefix6 3ffe:501:ffff:100:: 3ffe:501:ffff:111:: /64;
}

# A second subnet behind a relay agent
subnet6 3ffe:501:ffff:101::/64 {
    range6 [IPv6]10 3ffe:501:ffff:101::11;

    # Override of the global definitions,
    # works only when a resource (address or prefix) is assigned
    option dhcp6.name-servers [IPv6];

}

我搞砸了正则表达式

我从ihateregex.io站点借用了正则表达式来匹配IPv6。
在他们的网站上,当我用以前的相同输入样本测试它时,它完全匹配所有的地址,所以这就是我被卡住的地方。
在SO上有两个类似的问题,一个是用php,另一个是用python。如果可能的话,我想用javascript来做这一切,所以文本仍然是客户端的。

qojgxg4l

qojgxg4l1#

我从www.example.com站点借用了正则表达式来匹配IPv6。ihateregex.io site.
...但是您添加了\b到该正则表达式中。对于正则表达式开头的\b,这没有问题,但对于正则表达式的 * end *,则有问题。问题是IPv6地址可能以冒号结尾。在这种情况下,\b将要求最后一个冒号后跟字母数字!这就是文本中某些IPv6地址不匹配的原因。
所以...删除正则表达式末尾的\b
其次,如果期望有多个匹配,则需要提供g修饰符。
现在,无需将文本拆分为行:

var input = document.getElementById("input");
var output = document.getElementById("output");

function obfuscate() {
  output.value = input.value
        .replace(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g, "[IPv4]")
        .replace(/\b(([\da-f]{1,4}:){7}[\da-f]{1,4}|([\da-f]{1,4}:){1,7}:|([\da-f]{1,4}:){1,6}:[\da-f]{1,4}|([\da-f]{1,4}:){1,5}(:[\da-f]{1,4}){1,2}|([\da-f]{1,4}:){1,4}(:[\da-f]{1,4}){1,3}|([\da-f]{1,4}:){1,3}(:[\da-f]{1,4}){1,4}|([\da-f]{1,4}:){1,2}(:[\da-f]{1,4}){1,5}|[\da-f]{1,4}:((:[\da-f]{1,4}){1,6})|:((:[\da-f]{1,4}){1,7}|:)|fe80:(:[\da-f]{0,4}){0,4}%[\da-z]+|(::(ffff(:0{1,4})?:)?|([\da-f]{1,4}:){1,4}:)((25[0-5]|(2[0-4]|1?\d)?\d)\.){3}(25[0-5]|(2[0-4]|1?\d)?\d))/g,"[IPv6]");
}

input.value = `subnet6 3ffe:501:ffff:100::/64 {
    # Two addresses available to clients
    #  (the third client should get NoAddrsAvail)
    range6 3ffe:501:ffff:100::10 3ffe:501:ffff:100::11;

    # Use the whole /64 prefix for temporary addresses
    #  (i.e., direct application of RFC 4941)
    range6 3ffe:501:ffff:100:: temporary;

    # Some /64 prefixes available for Prefix Delegation (RFC 3633)
    prefix6 3ffe:501:ffff:100:: 3ffe:501:ffff:111:: /64;
}

# A second subnet behind a relay agent
subnet6 3ffe:501:ffff:101::/64 {
    range6 3ffe:501:ffff:101::10 3ffe:501:ffff:101::11;

    # Override of the global definitions,
    # works only when a resource (address or prefix) is assigned
    option dhcp6.name-servers 3ffe:501:ffff:101:200:ff:fe00:3f3e;

}`;
obfuscate();
table { border-collapse: collapse; margin: 10px 0 10px 0 }

textarea { width: 45vw; height: 75vh }
<table>
   <tr><th>Input Text<button onclick="obfuscate()">Obfuscate</button></th><th>Obfuscated Text</th></tr>
   <tr><td><textarea id="input"></textarea></td><td><textarea id="output"></textarea></tr>
</table>

相关问题