diff --git a/Specialized Areas/Regular Expressions/IP Address Validation/README.md b/Specialized Areas/Regular Expressions/IP Address Validation/README.md index 285bce4703..d67a83cc66 100644 --- a/Specialized Areas/Regular Expressions/IP Address Validation/README.md +++ b/Specialized Areas/Regular Expressions/IP Address Validation/README.md @@ -1,25 +1,24 @@ -The regex in the file getIP4OrIPV6address.js files validates for both ip address(ipv4) or ipv6 based on the input. +This snippet extracts IPv4 and IPv6 addresses from free text. For single-value validation, see `validateIPInput.js` and `Validate IPv6 Address/script.js`. -This regex cover the folloing IPv6 examples: -Full IPv6 addresses (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334). -Shorthand versions (e.g., fe80::1, where :: represents omitted zeros). -IPv6 with embedded IPv4 addresses (e.g., ::ffff:192.168.1.1). -And also includes following IPv4 addresses. -Following are the valid IP address examples: +The regex in `getIP4OrIPV6address.js` finds both IPv4 and IPv6 addresses within arbitrary text content. -192.168.1.1 +IPv6 coverage includes: +- Full addresses like `2001:0db8:85a3:0000:0000:8a2e:0370:7334` +- Compressed forms like `fe80::1` (`::` for omitted zeros) +- IPv4-embedded forms like `::ffff:192.168.1.1` -127.0.0.1 +IPv4 validation now strictly enforces each octet to be in the range 0–255. -0.0.0.0 +Valid IPv4 examples: -255.255.255.255 +- 192.168.1.1 +- 127.0.0.1 +- 0.0.0.0 +- 255.255.255.255 +- 1.2.3.4 -256.256.256.256 - -999.999.999.999 - -1.2.3 - -1.2.3.4 +Invalid IPv4 examples (correctly rejected by the regex): +- 256.256.256.256 +- 999.999.999.999 +- 1.2.3 diff --git a/Specialized Areas/Regular Expressions/IP Address Validation/getIP4OrIPV6address.js b/Specialized Areas/Regular Expressions/IP Address Validation/getIP4OrIPV6address.js index 2742dcf75b..71275dbea1 100644 --- a/Specialized Areas/Regular Expressions/IP Address Validation/getIP4OrIPV6address.js +++ b/Specialized Areas/Regular Expressions/IP Address Validation/getIP4OrIPV6address.js @@ -1,5 +1,26 @@ +// Extracts IPv4 and IPv6 addresses from arbitrary text content +// For single-value validation, use validateIPInput.js or Validate IPv6 Address/script.js extractIPAddresses: function(text) { - var ipRegex = /\b((\d{1,3}\.){3}\d{1,3})\b|\b([a-fA-F0-9:]+:+[a-fA-F0-9:]+)\b/g; - var matches = text.match(ipRegex); + var ipv4 = "(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"; + var ipv6 = "("+ + "(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|"+ + "(?:[A-Fa-f0-9]{1,4}:){1,7}:|"+ + "(?:[A-Fa-f0-9]{1,4}:){1,6}:[A-Fa-f0-9]{1,4}|"+ + "(?:[A-Fa-f0-9]{1,4}:){1,5}(?::[A-Fa-f0-9]{1,4}){1,2}|"+ + "(?:[A-Fa-f0-9]{1,4}:){1,4}(?::[A-Fa-f0-9]{1,4}){1,3}|"+ + "(?:[A-Fa-f0-9]{1,4}:){1,3}(?::[A-Fa-f0-9]{1,4}){1,4}|"+ + "(?:[A-Fa-f0-9]{1,4}:){1,2}(?::[A-Fa-f0-9]{1,4}){1,5}|"+ + "[A-Fa-f0-9]{1,4}:(?:(?::[A-Fa-f0-9]{1,4}){1,6})|"+ + ":(?:(?::[A-Fa-f0-9]{1,4}){1,7}|:)|"+ + "fe80:(?::[A-Fa-f0-9]{0,4}){0,4}%[0-9A-Za-z]{1,}|"+ + "::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:"+ + "(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"+ + ")|"+ + "(?:[A-Fa-f0-9]{1,4}:){1,4}:(?:"+ + "(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"+ + ")"+ + ")"; + var ipRegex = new RegExp("\\b(?:" + ipv4 + "|" + ipv6 + ")\\b","g"); + var matches = text.match(ipRegex); return matches; },