Skip to content

Commit 190c132

Browse files
Retrieves IPv4 or IPv6 IP addreses (#1427)
* Update readme.md The new retrieveIPv4orIPv6.js retrieves the IPv4 or IPv6 IP addresses. It is more robust in handling various IPs as stated in the readme file. * Create getIP4OrIPV6address.js The getIP4OrIPV6address.jsfunction extracts both IPv4 and IPv6 addresses from a given text. It uses a regular expression (ipRegex) to match patterns resembling IPv4 (four octets separated by dots) or IPv6 (hexadecimal digits separated by colons) addresses. The text.match(ipRegex) function searches the input text for all matching IP addresses and returns them as an array. If no matches are found, the result will be null. This regex covers the following: 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).
1 parent bcbf80c commit 190c132

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
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+
return matches;
5+
},

Regular Expressions/IP Address Validation/readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
This regex validates for ip address(ipv4) based on the input.
1+
The regex in the file getIP4OrIPV6address.js files validates for both ip address(ipv4) or ipv6 based on the input.
22

3+
This regex cover the folloing IPv6 examples:
4+
Full IPv6 addresses (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334).
5+
Shorthand versions (e.g., fe80::1, where :: represents omitted zeros).
6+
IPv6 with embedded IPv4 addresses (e.g., ::ffff:192.168.1.1).
7+
And also includes following IPv4 addresses.
38
Following are the valid IP address examples:
49

510
192.168.1.1

0 commit comments

Comments
 (0)