I'm trying to check strings to see it they are a valid IP address:
export function checkIpIsValid(ip) {
const regex = new RegExp("^([1-2]?[0-9]{1,2}\.){3}[1-2]?[0-9]{1,2}$")
return regex.test(ip);
}
But this test is failing because it return true:
expect(checkIpIsValid("1.1.1.1")).toBe(true);
expect(checkIpIsValid("152.154.22.35")).toBe(true);
expect(checkIpIsValid("552.154.22.35")).toBe(false);
// These three are fine
// But the one below is not, it doesn't contain 4 parts so it should return false. Instead, it is returning true.
expect(checkIpIsValid("154.22.35")).toBe(false);
// Expected value to be (using ===):
// false
// Received:
// true
The thing is, when I check it on https://regex101.com/ it works fine on that case...