Reputation: 50362
Looking for a good IP address parser for Javascript.
Ideally, it could take in an IP address as a string, then return an object containing all of the pieces of the IP Address, including the port.
Thanks!
Upvotes: 12
Views: 8803
Reputation: 1581
I came across this question while implementing Safe Browsing url canonicalization in JS. The answers here are helpful, and here's some JS I came up with that does octal & hex IP addresses, in case it's helpful to anyone else in the future:
function verifyIP4(address) {
var ip4DecimalPattern = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))$';
var ip4HexPattern = '^(?:(?:0x[0-9a-f]{1,2})\.){3}(?:0x[0-9a-f]{1,2})$';
var ip4OctalPattern = '^(?:(?:03[1-7][0-7]|0[12][0-7]{1,2}|[0-7]{1,2})\.){3}(?:03[1-7][0-7]|0[12][0-7]{1,2}|[0-7]{1,2})$';
var isIP4Decimal = isIP4Hex = isIP4Octal = false;
var base = 10;
isIP4Decimal = address.match(ip4DecimalPattern) != null;
isIP4Hex = address.match(ip4HexPattern) != null;
isIP4Octal = address.match(ip4OctalPattern) != null;
if (isIP4Hex || isIP4Octal) {
if (isIP4Hex) {
base = 16;
} else if (isIP4Octal) {
base = 8;
}
return address.split('.').map(num => parseInt(num, base)).join('.');
}
return false;
}
Upvotes: 1
Reputation: 50362
This is what I came up with.
::
// REGEX to break an ip address into parts
var ip_regex = /(\d+)\.(\d+)\.(\d+)\.(\d+)(?:\.(\d+))?(?::(\d+))?/ig;
// Parse the ip string into an object containing it's parts
var parse_ip_address = function(ip_string){
// Use Regex to get the parts of the ip address
var ip_parts = ip_regex.exec(ip_string);
// Set ip address if the regex executed successfully
if( ip_parts && ip_parts.length > 6 ){
// Set up address object to elements in the list
var ip_address = {
'A': ip_parts[1],
'B': ip_parts[2],
'C': ip_parts[3],
'D': ip_parts[4],
'E': ip_parts[5],
'port': ip_parts[6]
}
// Would rather not fiddle with 'undefined' value
if( typeof ip_parts[5] != 'undefined') {
ip_address[5] = null;
}
}
// Return object
return ip_parts;
};
Upvotes: 0
Reputation: 35533
var v4 = '[\\d]{1-3}';
var v4d = '\\.';
var v4complete = v4+v4d+v4+v4d+v4+v4d+v4
var v6 = '[\\da-fA-F]{0-4}';
var v6d = ':';
var v6complete = v6+v6d+v6+v6d+v6+v6d+v6+v6d+v6+v6d+v6+v6d+v6+v6d+v6;
var regex = new RegExp('(' + v4complete + '(\\:\d+){0,1}|'
+ '::|::1|'
+ '\\[::\\]:\\d+|\\[::1\\]:\\d+|'
+ v6complete + '|'
+ '\\[' + v6complete + '\\]:\\d+' + ')', 'g');
var result = mystring.match(regex);
Note that this doesn't guarantee valid addresses (in the range 0-255 for IPv4, for example). But it should match ip's with or without the port.
Upvotes: 2
Reputation: 53119
function parseIP(ip) {
if(ip.match(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3})/)!=null) {
ip = ip.match(/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/); //clean posible port or http://
return ip.split("."); //returns [a,b,c,d] array
}
else
return false;
}
That will do it. Split method splits string by delimiter. Its opposite is Array.join(delimiter)
, which joins the array with optional delimiter
between the pieces.
Upvotes: 2