Reputation: 1762
I have this 'broker' discovery service, where it allows the user to find what the private local IP address of their product is. This product uploads it's private local IP address along with the IPV4 address and IPV6 address of the network it's on to a database.
When this user reaches to my web app (discovery.example.com), it will get either the user's IPV4/IPV6 address (whatever way they connect through) and check the database if there are any matches with that IP address, if there are then return the associated private local IP address to the user, then the user has their local IP address of their product in their home network.
Obviously each IPV6 address from each device will be different at the ending parts, so I was wondering on how you would get the first four bytes of data of an IPV6 address (as that's their network ID)?
Say I got this for an address (this is randomly made, don't try searching it up as it'll lead to nowhere):
7g98:i9e:k897:a388:908a:j983:d7h7:98o0
I want to split it up so I only get:
7g98:i9e:k897:a388
So whatever device they check on, it still knows its the same network?
Thanks.
Upvotes: 0
Views: 494
Reputation: 1142
Here is what you want:
const ip = "7g98:i9e:k897:a388:908a:j983:d7h7:98o0";
const match = ip.match(/((.*?):){4}/)
if (match) {
console.log(match[0].slice(0,-1))
}
Upvotes: 1