|
| 1 | +// Extracts IPv4 and IPv6 addresses from arbitrary text content |
| 2 | +// For single-value validation, use validateIPInput.js or Validate IPv6 Address/script.js |
1 | 3 | extractIPAddresses: function(text) { |
2 | | - var ipRegex = /\b((\d{1,3}\.){3}\d{1,3})\b|\b([a-fA-F0-9:]+:+[a-fA-F0-9:]+)\b/g; |
3 | | - var matches = text.match(ipRegex); |
| 4 | + var ipv4 = "(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"; |
| 5 | + var ipv6 = "("+ |
| 6 | + "(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|"+ |
| 7 | + "(?:[A-Fa-f0-9]{1,4}:){1,7}:|"+ |
| 8 | + "(?:[A-Fa-f0-9]{1,4}:){1,6}:[A-Fa-f0-9]{1,4}|"+ |
| 9 | + "(?:[A-Fa-f0-9]{1,4}:){1,5}(?::[A-Fa-f0-9]{1,4}){1,2}|"+ |
| 10 | + "(?:[A-Fa-f0-9]{1,4}:){1,4}(?::[A-Fa-f0-9]{1,4}){1,3}|"+ |
| 11 | + "(?:[A-Fa-f0-9]{1,4}:){1,3}(?::[A-Fa-f0-9]{1,4}){1,4}|"+ |
| 12 | + "(?:[A-Fa-f0-9]{1,4}:){1,2}(?::[A-Fa-f0-9]{1,4}){1,5}|"+ |
| 13 | + "[A-Fa-f0-9]{1,4}:(?:(?::[A-Fa-f0-9]{1,4}){1,6})|"+ |
| 14 | + ":(?:(?::[A-Fa-f0-9]{1,4}){1,7}|:)|"+ |
| 15 | + "fe80:(?::[A-Fa-f0-9]{0,4}){0,4}%[0-9A-Za-z]{1,}|"+ |
| 16 | + "::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:"+ |
| 17 | + "(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"+ |
| 18 | + ")|"+ |
| 19 | + "(?:[A-Fa-f0-9]{1,4}:){1,4}:(?:"+ |
| 20 | + "(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"+ |
| 21 | + ")"+ |
| 22 | + ")"; |
| 23 | + var ipRegex = new RegExp("\\b(?:" + ipv4 + "|" + ipv6 + ")\\b","g"); |
| 24 | + var matches = text.match(ipRegex); |
4 | 25 | return matches; |
5 | 26 | }, |
0 commit comments