题解 | #验证IP地址#
验证IP地址
http://www.nowcoder.com/practice/55fb3c68d08d46119f76ae2df7566880
/**
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
function solve( IP ) {
// write code here
const arr4 = IP.split(".")
const arr6 = IP.split(":")
const ex4 = /^0$|^[1-9]\d{0,2}$/
const ex6 = /^[0-9a-fA-F]{1,4}$/
if(arr4.length==4 && arr4.every(item => item.match(ex4) && item < 256)){
return "IPv4"
}else if(arr6.length==8 && arr6.every(item => item.match(ex6)) ){
return "IPv6"
}
return "Neither"
}
module.exports = {
solve : solve
};