题解 | #验证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,
};

全部评论

相关推荐

点赞 评论 收藏
分享
06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务