0

I am trying to use a regex to match a ip string using the following code:

function isValidIP(str) {
  const arr = str.split('.')
  let count = 0
  if(arr && arr.length === 4) {
    const reg = /((?:\b)(0|[1-9]{1,2}[0]?)\b)|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])/g
    for(let i =0 ; i< arr.length ; i++) {
      console.log(reg.test(arr[i]))
      if(reg.test(arr[i]) === true) {
        console.log(count)
        count++
      }
    }
    if(count === 4)  {return true} else return false
  } else {
    return false
  }
}

The log for reg.test(arr[i]) gives true as output but even then the if condition block is not being accessed as no log is being prited for count? i would really appreciate some help here

risky last
  • 385
  • 4
  • 12

0 Answers0