0

My intention is to get the following validated using regex

10.10.*.1
10.10.0.1
10.10.255.1
10.10.10.*
10.10.10.0
10.10.10.255

In simple terms, a star can appear in the last two octets and the IP address would still be valid

My code is as follows:

function ValidateIPaddress(ipaddress) {  
  if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|\*?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|\*?)$/.test(ipaddress)) {  
    return (true)  
  }
  return (false)  
}  

I am unable to set an or condition in the last two octets. Please help

3
  • 1
    You may use /^\d+\.\d+(\.(?:\*|\d+)){2}$/ Commented Mar 20, 2018 at 16:20
  • @anubhava: That is considerably less restrictive than what the OP is aiming for. Commented Mar 20, 2018 at 16:23
  • There is very little reason of keeping it so restrictive unless a user is entering this IP :) Commented Mar 20, 2018 at 16:25

3 Answers 3

2

Method 1 - regex simplified

No need to overcomplicate the regex. Just ensure that the string conforms with the general format (the following regex) and then test that each octet is < 256 as the code snippet below shows.

^\d{1,3}\.\d{1,3}(?:\.(?:\d{1,3}|\*)){2}$
  • ^ Assert position at the start of the line
  • \d{1,3} Match a digit 1 to 3 times
  • \. Match . literally
  • \d{1,3} Match a digit 1 to 3 times
  • (?:\.(?:\d{1,3}|\*)){2} Match the following twice
    • \. Match . literally
    • (?:\d{1,3}|\*) Match either a digit 1 to 3 times or * literally\
  • $ Assert position at the end of the line

var a = [
  // valid
  "10.10.*.1",
  "10.10.0.1",
  "10.10.255.1",
  "10.10.10.*",
  "10.10.10.0",
  "10.10.10.255",
  // invalid
  "256.1.1.1",
  "*.1.1.1",
  "1.1.1",
  "1.1.1.1.1"
]

var r = /^\d{1,3}\.\d{1,3}(?:\.(?:\d{1,3}|\*)){2}$/

a.forEach(function(ip) {
    console.log(`${ip}: ${r.test(ip) && ip.split('.').every(function(x) { return Number(x) < 256 || x === '*' })}`)
})


Method 2 - no regex

Alternatively, without even using regex:

var a = [
  // valid
  "10.10.*.1",
  "10.10.0.1",
  "10.10.255.1",
  "10.10.10.*",
  "10.10.10.0",
  "10.10.10.255",
  // invalid
  "256.1.1.1",
  "*.1.1.1",
  "1.1.1",
  "1.1.1.1.1"
]

a.forEach(function(ip) {
  var octets = ip.split('.'),
      valid = false
  if(octets.length === 4) {
    if(Number(octets[0]) < 256
      && Number(octets[1]) < 256
      && (Number(octets[2]) < 256 || octets[2] === '*')
      && (Number(octets[3]) < 256 || octets[3] === '*')
    )
      valid = true
  }
  console.log(`${ip}: ${valid}`)
})


Method 3 - single regex

This is the most bloated method, but it seems that's what you're looking for.

^(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2}|\*)){2}$

var a = [
  // valid
  "10.10.*.1",
  "10.10.0.1",
  "10.10.255.1",
  "10.10.10.*",
  "10.10.10.0",
  "10.10.10.255",
  // invalid
  "256.1.1.1",
  "*.1.1.1",
  "1.1.1",
  "1.1.1.1.1"
]

var r = /^(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2}|\*)){2}$/

a.forEach(function(ip) {
  console.log(`${ip}: ${r.test(ip)}`)
})

Sign up to request clarification or add additional context in comments.

2 Comments

++ that's the way to go
@anubhava it wasn't enough to detour the OP unfortunately haha
1

You are missing on the last two octets anything to capture the single digit possibility. Try:

^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|[0-9]|\*?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|[0-9]|\*?)$

Comments

1

I think this should do it.

let rx = /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]|\*)\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]|\*)$/;

const test = str => rx.test(str)

console.log("these should pass")
console.log(test("10.10.*.1"))
console.log(test("10.10.0.1"))
console.log(test("10.10.255.1"))
console.log(test("10.10.10.*"))
console.log(test("10.10.10.0"))
console.log(test("10.10.10.255"))

console.log("------------------------------")
console.log("these should fail")
console.log(test("10.267.0.1"))
console.log(test("10.10.0.1.0.1"))
console.log(test("10.*.0.1"))
console.log(test("*.42.0.1"))

But, as @ctwheels points out, this might be better with less regex.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.