Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
The regex in the file getIP4OrIPV6address.js files validates for both ip address(ipv4) or ipv6 based on the input.
The regex in `getIP4OrIPV6address.js` validates both IPv4 and IPv6 addresses in input text.

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:
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`

192.168.1.1
IPv4 validation now strictly enforces each octet to be in the range 0–255.

127.0.0.1
Valid IPv4 examples:

0.0.0.0
- 192.168.1.1
- 127.0.0.1
- 0.0.0.0
- 255.255.255.255
- 1.2.3.4

255.255.255.255

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
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
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;
},
Loading