TheoKondak
TheoKondak

Reputation: 9

Get substings between Specific Characters

I am trying to solve some JS problem. I want to check if an IP address is a valid one. So the numbers must be between 0-255.

So what I want to do at this point, is to get an IP ex 192.168.1.1 and get substrings and load them to an array, so I want to create an array that looks like that:

array = ['192' , '168' , '1' , '1'];

I've tried various approaches in my algorithm but can't manage to target dynamically the numbers and split them between every dot.

I've done several tries, and thats the closest I could get.

let str = '192.168.1.1';

isValidIp(str);

function isValidIP(str) {
let array = [];
let substringArray = [];
for (let i=0; i<str.length; i++){

if (str[i] == '.') array.push(i);

}

let counter = 0;
for (let i in array){
substringArray.push(str.substring(counter, array[i]));
counter = array[i];
}
console.log(substringArray);
}


Which returns:

[ '192', '.168', '.1' ]

Upvotes: 0

Views: 80

Answers (4)

Booboo
Booboo

Reputation: 44013

function isValidIP(str) {
    let re = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
    let m = str.match(re);
    return m &&
           m[1] >= 0 && m[1] <= 255 &&
           m[2] >= 0 && m[2] <= 255 &&
           m[3] >= 0 && m[3] <= 255 &&
           m[4] >= 0 && m[4] <= 255
           ;
}

If you wish to be more precise, each digit check can be:

(0|[1-9]\d{0:2})

This prevents extraneous leading 0's.

Upvotes: 0

qelli
qelli

Reputation: 2077

You can use the split() function of JavaScript which returns an array of every element separated by the digit specified. Or, which I wouldn't recommend, you could use RegEx. Here is an example of both:

function isValidIPwRegEx(str){
 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(str))
  {
    return true;
  }
return false;
}

function isValidIP(str) {
    let array = str.split("."),
        isIP = true;
    array = array.filter( block => !block.includes("+") && !block.includes("e") );
    if(array.length!=4) return false;
    array.forEach((number) => {
        if ( !(+number >=0 && +number <= 255) ) { //As @p.s.w.g kindly suggested
            isIP = false;
        }
    });
    return isIP;
}

//With RegEx
console.log("With RegEx");
console.log(isValidIPwRegEx("192.168.1.1"));
console.log(isValidIPwRegEx("blah.blah.blah.blah")); //As @georg suggested
console.log(isValidIPwRegEx("1e1.2e1.+3e1.+5e1")); //As @georg again suggested to @Nina Scholz
console.log("");
//Without RegEx
console.log("Without RegEx");
console.log(isValidIP("192.168.1.1"));
console.log(isValidIP("blah.blah.blah.blah")); //As @georg suggested
console.log(isValidIP("1e1.2e1.+3e1.+5e1")); //As @georg again suggested to @Nina Scholz
console.log(isValidIP("1e1.2e1.3e1.5e1"));

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386520

You could split the string and check if the length is four and all values are integers and smaller than 256.

var ip = '192.168.1.1',
    values = ip.split('.'),
    valid = values.length === 4 && values.every(v => +v >= 0 && +v < 256);

console.log(values);
console.log(valid);

Upvotes: 0

LA Imagine
LA Imagine

Reputation: 29

Use String's split function.

So, something like "192.168.1.1".split(".")

Upvotes: 0

Related Questions