addIpToArray() { let newIp = ''; isEmpty(this.mask) ? newIp = this.ip : newIp = this.ip + '/' + this.mask; console.log(newIp) let test = this.regexp.test(newIp) console.log(test) } 

It is necessary to validate aypishnik with a mask. Returns true, false, true, false. those. at each iteration a different value. What could be the problem? Regular:

 /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))?$/g 
  • Give enough complete code so that we run it and get these most true false false false - andreymal
  • It seems to me that the forum has already had a question about IP validation. - nick_n_a
  • @andreymal, it's clear here without it. - Qwertiy
  • @nick_n_a, I think this question is about a regular schedule for checking ip. - Qwertiy
  • Here there is a regular check IP ru.stackoverflow.com/questions/706722/… - nick_n_a

1 answer 1

What could be the problem?

Remove the regular flag from g . Because of him, she tries to continue searching from the last entry instead of checking the string.

 var r1 = /^.$/, r2 = /^.$/g; console.log(["a", "b", "c", "d"].map(s => r1.test(s)).join(" ")); console.log(["a", "b", "c", "d"].map(s => r2.test(s)).join(" "));