I am validating an array of IPv4 addresses using javascript regex.
I am getting unexpected result when I use the global
flag.
I run the code using Node 10 & 13 and on chrome browser and they all gave the same result.
var ips = ['22.231.113.64', '22.231.113.164', '22.231.113.164']
const pv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?\d{1,2})\.){3}(?:25[0-5]|2[0-4][0-9]|[0-1]?\d{1,2})$/g
console.log(
ips.map(x => pv4.test(x))
)
the above code is giving me
[true, false, true]
Notice that the one ip that gave me false is exactly the same as the one behind it that gave me true (on index 1,2)
If I run it without the global flag, and I got what I expect
[true, true, true]