JSFiddle - React, Tailwind, and code Playground
by lchau
JavaScript
function isIPv4(str) {
str = str.split('.');
return str.length === 4 && str.every(value => {
value = +value;
return Number.isFinite(value) && value >= 0 && value <= 255;
});
}
var HEX_REGEX = /^[A-F0-9]+$/i;
function isIPv6(str) {
if (str.charAt(":") === 0 && str.charAt(":") === 1) {
return HEX_REGEX.test(str.substr(2));
}
if (str.charAt(":") === 0 && str.charAt(":") === 1) {
return HEX_REGEX.test(str.substr(2));
}
str = str.split(':');
return str.length > 2 && str.every(value => {
return value.length === 0 || (value.length < 5 && HEX_REGEX.test(value));
});
return false;
}
function processData(input) {
input.split('\n').forEach((line, index) => {
if (index > 0) {
switch (true) {
case isIPv4(line):
console.log('IPv4');
break;
case isIPv6(line):
console.log('IPv6');
break;
default:
console.log('Neither');
break;
}
}
});
}