82

I have several ip addresses like:

  1. 115.42.150.37
  2. 115.42.150.38
  3. 115.42.150.50

What type of regular expression should I write if I want to search for the all the 3 ip addresses? Eg, if I do 115.42.150.* (I will be able to search for all 3 ip addresses)

What I can do now is something like: /[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/ but it can't seems to work well.

Thanks.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
KennC.
  • 3,315
  • 6
  • 20
  • 18
  • possible duplicate of [How to evaluate an IP?](http://stackoverflow.com/questions/42345/how-to-evaluate-an-ip) – Pekka Dec 16 '10 at 12:10
  • The dupe link has a good regex by Bill The Lizard – Pekka Dec 16 '10 at 12:10
  • 1
    I agree with Pekka, that the linked question should cover your requirements exactly. – Andrzej Doyle Dec 16 '10 at 12:16
  • Please use my answer from this post: https://stackoverflow.com/questions/23483855/javascript-regex-to-validate-ipv4-and-ipv6-address-no-hostnames/69685444#69685444, it's the most accurate one so far. – Mecanik Oct 23 '21 at 05:51

25 Answers25

138

May be late but, someone could try:

Example of VALID IP address

115.42.150.37
192.168.0.1
110.234.52.124

Example of INVALID IP address

210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – only digits are allowed
255.0.0.y – only digits are allowed
666.10.10.20 – octet number must be between [0-255]
4444.11.11.11 – octet number must be between [0-255]
33.3333.33.3 – octet number must be between [0-255]

JavaScript code to validate an IP address

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)  
  }  
  alert("You have entered an invalid IP address!")  
  return (false)  
}  
tanmay
  • 7,761
  • 2
  • 19
  • 38
ErickBest
  • 4,586
  • 5
  • 31
  • 43
  • 7
    This worked great, I appreciate you putting in a whole function and examples of what will/will not pass. – SpaceNinja Mar 03 '15 at 15:34
  • 1
    Your regex was the only one I could get to pass my tests. Thanks! – TWright Jan 24 '17 at 03:43
  • Glad I could help. Enjoy Coding!! – ErickBest Mar 21 '17 at 10:20
  • 1
    Visualization of @ErickBest 's answer:https://jex.im/regulex/#!flags=&re=%5E(25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F)%5C.(25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F)%5C.(25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F)%5C.(25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F)%24 – vikyd Dec 24 '18 at 11:15
  • 2
    Your solution should not allow '012.012.012.012' – Mahdi Pedram Feb 18 '19 at 06:58
  • According to your solution, '00.00.00.00' or '000.000.000.000' are allowed, but I don't think they should be. – LHCHIN Mar 06 '19 at 05:35
  • A more precise REGEX is: ^(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])$ https://www.regular-expressions.info/ip.html – unixeo Nov 15 '19 at 21:30
  • Your solution allowed 123.045.067.089 for example. Leading zeros are not valid ("123.045.067.089"should return false). – Ali Zedan Jan 22 '21 at 08:34
  • '0.10.10.10' also accepted by this solution – user2956477 Apr 11 '21 at 12:07
  • this solution does not work for IPV6 – Andrei Nov 08 '21 at 21:07
  • You can combine this solution with others for a perfect outcome – ErickBest Jan 22 '22 at 02:49
  • @AliZedan Solution based on OP question – ErickBest Jan 22 '22 at 02:50
  • @Andrei Solution based on OP question. – ErickBest Jan 22 '22 at 02:51
61

Try this one, it's a shorter version:

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

Explained:

^ start of string
  (?!0)         Assume IP cannot start with 0
  (?!.*\.$)     Make sure string does not end with a dot
  (
    (
    1?\d?\d|   A single digit, two digits, or 100-199
    25[0-5]|   The numbers 250-255
    2[0-4]\d   The numbers 200-249
    )
  \.|$ the number must be followed by either a dot or end-of-string - to match the last number
  ){4}         Expect exactly four of these
$ end of string

Unit test for a browser's console:

var rx=/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/;
var valid=['1.2.3.4','11.11.11.11','123.123.123.123','255.250.249.0','1.12.123.255','127.0.0.1','1.0.0.0'];
var invalid=['0.1.1.1','01.1.1.1','012.1.1.1','1.2.3.4.','1.2.3\n4','1.2.3.4\n','259.0.0.1','123.','1.2.3.4.5','.1.2.3.4','1,2,3,4','1.2.333.4','1.299.3.4'];
valid.forEach(function(s){if (!rx.test(s))console.log('bad valid: '+s);});
invalid.forEach(function(s){if (rx.test(s)) console.log('bad invalid: '+s);});
oriadam
  • 7,747
  • 2
  • 50
  • 48
  • 4
    `0.0.0.0` is assumed to be valid – Dan K.K. Dec 16 '15 at 11:49
  • 4
    In that case you can omit the negative look-ahead `(?!0)` – oriadam Dec 17 '15 at 16:35
  • What about allowing subnets? pe: 192.168.1.10/24 – migueloop Apr 26 '18 at 07:37
  • @migueloop I didnt try it: `^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}(\/\d+)?$` – oriadam Apr 30 '18 at 11:24
  • I find this regex will cover ips and subnets. and make sure it will not allow leading 0 in each block. /^(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])(\/([1-2][0-9]|3[0-2]|[0-9]))?$/ – Rob Jun 19 '19 at 15:20
  • @DanK.K. `0.0.0.0` is indeed valid https://en.wikipedia.org/wiki/0.0.0.0 – undefined Sep 30 '21 at 19:07
45

If you are using nodejs try:

require('net').isIP('10.0.0.1')

doc net.isIP()

Andrew K
  • 646
  • 5
  • 6
  • 2
    I like this one. This one is the best though maybe technically it doesn't answer the question exactly. Easy to use and easy to read. Many thanks! – Harlin Apr 18 '19 at 22:11
  • Extracted the REGEX used in isIP() https://stackoverflow.com/a/68104187/9387542 – abhijithvijayan Jun 23 '21 at 17:03
33

The regex you've got already has several problems:

Firstly, it contains dots. In regex, a dot means "match any character", where you need to match just an actual dot. For this, you need to escape it, so put a back-slash in front of the dots.

Secondly, but you're matching any three digits in each section. This means you'll match any number between 0 and 999, which obviously contains a lot of invalid IP address numbers.

This can be solved by making the number matching more complex; there are other answers on this site which explain how to do that, but frankly it's not worth the effort -- in my opinion, you'd be much better off splitting the string by the dots, and then just validating the four blocks as numeric integer ranges -- ie:

if(block >= 0 && block <= 255) {....}

Hope that helps.

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
Spudley
  • 166,037
  • 39
  • 233
  • 307
21

Don't write your own regex or copy paste! You probably won't cover all edge cases (IPv6, but also octal IPs, etc). Use the is-ip package from npm:

var isIp = require('is-ip');

isIp('192.168.0.1');

isIp('1:2:3:4:5:6:7:8');

Will return a Boolean.

E_net4
  • 27,810
  • 13
  • 101
  • 139
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
16

Try this one.. Source from here.

"\b(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]?)\b"
Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
  • 1
    /^(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('10.10.10.10') – framp May 16 '13 at 08:17
15

Below Solution doesn't accept Padding Zeros

Here is the cleanest way to validate an IP Address, Let's break it down:

Fact: a valid IP Address is has 4 octets, each octets can be a number between 0 - 255

Breakdown of Regex that matches any value between 0 - 255

  • 25[0-5] matches 250 - 255
  • 2[0-4][0-9] matches 200 - 249
  • 1[0-9][0-9] matches 100 - 199
  • [1-9][0-9]? matches 1 - 99
  • 0 matches 0
const octet = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)';

Notes: When using new RegExp you should use \\. instead of \. since string will get escaped twice.

function isValidIP(str) {
  const octet = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)';
  const regex = new RegExp(`^${octet}\\.${octet}\\.${octet}\\.${octet}$`);
  return regex.test(str);
}
Mahdi Pedram
  • 689
  • 6
  • 12
13

If you want something more readable than regex for ipv4 in modern browsers you can go with

function checkIsIPV4(entry) {
  var blocks = entry.split(".");
  if(blocks.length === 4) {
    return blocks.every(function(block) {
      return parseInt(block,10) >=0 && parseInt(block,10) <= 255;
    });
  }
  return false;
}
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
  • 4
    The IP `200sd.100f.85.200(V)` (or any with letters in it) is returning `true` in your function. Just check also if `!isNaN(block)` on each block to avoid this. Nice funtion BTW. `return !isNaN(block) && parseInt(block,10) >=0 && parseInt(block,10) <= 255;` – Alvaro Flaño Larrondo Feb 05 '15 at 22:19
  • I think the function should be implemented as: `function isIPv4Address(entry) { var blocks = entry.split("."); if(blocks.length === 4) { return blocks.every(function(block) { const value = parseInt(block, 10); if(value >= 0 && value <= 255){ var i = block.length; while (i--) { if(block[i] < '0' || block[i] > '9'){ return false; } } return true; } }); } return false; }` – anhldbk Mar 24 '16 at 11:04
  • Leading zeros are not valid ("123.045.067.089" should return false), and your solution allowed leading zero, and that it is not correct. – Ali Zedan Jan 22 '21 at 08:41
8

A short RegEx: ^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4}$

Example

const isValidIp = value => (/^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4}$/.test(value));


// valid
console.log("isValidIp('0.0.0.0') ? ", isValidIp('0.0.0.0'));
console.log("isValidIp('115.42.150.37') ? ", isValidIp('115.42.150.37'));
console.log("isValidIp('192.168.0.1') ? ", isValidIp('192.168.0.1'));
console.log("isValidIp('110.234.52.124') ? ", isValidIp('110.234.52.124'));
console.log("isValidIp('115.42.150.37') ? ", isValidIp('115.42.150.37'));
console.log("isValidIp('115.42.150.38') ? ", isValidIp('115.42.150.38'));
console.log("isValidIp('115.42.150.50') ? ", isValidIp('115.42.150.50'));
console.log("isValidIp('192.168.1.70') ? ", isValidIp('192.168.1.70'));

// Invalid
console.log("isValidIp('210.110') ? ", isValidIp('210.110'));
console.log("isValidIp('255') ? ", isValidIp('255'));
console.log("isValidIp('y.y.y.y') ? ", isValidIp('y.y.y.y'));
console.log("isValidIp('255.0.0.y') ? ", isValidIp('255.0.0.y'));
console.log("isValidIp('666.10.10.20') ? ", isValidIp('666.10.10.20'));
console.log("isValidIp('4444.11.11.11') ? ", isValidIp('4444.11.11.11'));
console.log("isValidIp('33.3333.33.3') ? ", isValidIp('33.3333.33.3'));
console.log("isValidIp('0192.168.1.70') ? ", isValidIp('0192.168.1.70'));
Kayden van Rijn
  • 1,939
  • 2
  • 6
  • 14
Ala Eddine JEBALI
  • 7,033
  • 6
  • 46
  • 65
4
/^(?!.*\.$)((?!0\d)(1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/

Full credit to oriadam. I would have commented below his/her answer to suggest the double zero change I made, but I do not have enough reputation here yet...

change:

Community
  • 1
  • 1
Tom
  • 4,776
  • 2
  • 38
  • 50
4

Simple Method

const invalidIp = ipAddress
   .split(".")
   .map(ip => Number(ip) >= 0 && Number(ip) <= 255)
   .includes(false);

if(invalidIp){
 // IP address is invalid
 // throw error here
}
Mac Ignacio
  • 655
  • 7
  • 5
3

Regular expression for the IP address format:

/^(\d\d?)|(1\d\d)|(0\d\d)|(2[0-4]\d)|(2[0-5])\.(\d\d?)|(1\d\d)|(0\d\d)|(2[0-4]\d)|(2[0-5])\.(\d\d?)|(1\d\d)|(0\d\d)|(2[0-4]\d)|(2[0-5])$/;
pearcoding
  • 1,149
  • 1
  • 9
  • 28
3

If you wrtie the proper code you need only this very simple regular expression: /\d{1,3}/

function isIP(ip) {
    let arrIp = ip.split(".");
    if (arrIp.length !== 4) return "Invalid IP";
    let re = /\d{1,3}/;
    for (let oct of arrIp) {
        if (oct.match(re) === null) return "Invalid IP"
        if (Number(oct) < 0 || Number(oct) > 255)
            return "Invalid IP";
}
    return "Valid IP";
}

But actually you get even simpler code by not using any regular expression at all:

function isIp(ip) {
    var arrIp = ip.split(".");
    if (arrIp.length !== 4) return "Invalid IP";
    for (let oct of arrIp) {
        if ( isNaN(oct) || Number(oct) < 0 || Number(oct) > 255)
            return "Invalid IP";
}
    return "Valid IP";
}
3

Throwing in a late contribution:

^(?!\.)((^|\.)([1-9]?\d|1\d\d|2(5[0-5]|[0-4]\d))){4}$

Of the answers I checked, they're either longer or incomplete in their verification. Longer, in my experience, means harder to overlook and therefore more prone to be erroneous. And I like to avoid repeating similar patters, for the same reason.

The main part is, of course, the test for a number - 0 to 255, but also making sure it doesn't allow initial zeroes (except for when it's a single one):

[1-9]?\d|1\d\d|2(5[0-5]|[0-4]\d)

Three alternations - one for sub 100: [1-9]?\d, one for 100-199: 1\d\d and finally 200-255: 2(5[0-5]|[0-4]\d).

This is preceded by a test for start of line or a dot ., and this whole expression is tested for 4 times by the appended {4}.

This complete test for four byte representations is started by testing for start of line followed by a negative look ahead to avoid addresses starting with a .: ^(?!\.), and ended with a test for end of line ($).

See some samples here at regex101.

SamWhan
  • 8,296
  • 1
  • 18
  • 45
3

This is what I did and it's fast and works perfectly:

function isIPv4Address(inputString) {
    let regex = new RegExp(/^(([0-9]{1,3}\.){3}[0-9]{1,3})$/);
    if(regex.test(inputString)){
        let arInput = inputString.split(".")
        for(let i of arInput){
            if(i.length > 1 && i.charAt(0) === '0')
                return false;
            else{
                if(parseInt(i) < 0 || parseInt(i) >=256)
                   return false;
            }
        }
    }
    else
        return false;
    return true;
}

Explanation: First, with the regex check that the IP format is correct. Although, the regex won't check any value ranges.

I mean, if you can use Javascript to manage regex, why not use it?. So, instead of using a crazy regex, use Regex only for checking that the format is fine and then check that each value in the octet is in the correct value range (0 to 255). Hope this helps anybody else. Peace.

SCouto
  • 7,808
  • 5
  • 32
  • 49
2

And instead of

{1-3}

you should put

{1,3}
Mikel
  • 24,855
  • 8
  • 65
  • 66
1

it is maybe better:

function checkIP(ip) {
    var x = ip.split("."), x1, x2, x3, x4;

    if (x.length == 4) {
        x1 = parseInt(x[0], 10);
        x2 = parseInt(x[1], 10);
        x3 = parseInt(x[2], 10);
        x4 = parseInt(x[3], 10);

        if (isNaN(x1) || isNaN(x2) || isNaN(x3) || isNaN(x4)) {
            return false;
        }

        if ((x1 >= 0 && x1 <= 255) && (x2 >= 0 && x2 <= 255) && (x3 >= 0 && x3 <= 255) && (x4 >= 0 && x4 <= 255)) {
            return true;
        }
    }
    return false;
}    
1
\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b

matches 0.0.0.0 through 999.999.999.999 use if you know the seachdata does not contain invalid IP addresses

\b(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]?)\b

use to match IP numbers with accurracy - each of the 4 numbers is stored into it's own capturing group, so you can access them later

macf00bar
  • 673
  • 1
  • 14
  • 32
1

The answers over allow leading zeros in Ip address, and that it is not correct. For example ("123.045.067.089"should return false).

The correct way to do it like that.

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

This function will not allow zero to lead IP addresses.

Ali Zedan
  • 285
  • 3
  • 17
0

Always looking for variations, seemed to be a repetitive task so how about using forEach!

function checkIP(ip) {
  //assume IP is valid to start, once false is found, always false
  var test = true;

  //uses forEach method to test each block of IPv4 address
  ip.split('.').forEach(validateIP4);

  if (!test) 
    alert("Invalid IP4 format\n"+ip) 
  else 
    alert("IP4 format correct\n"+ip);

  function validateIP4(num, index, arr) {
    //returns NaN if not an Int
    item = parseInt(num, 10);
    //test validates Int, 0-255 range and 4 bytes of address
    // && test; at end required because this function called for each block
    test = !isNaN(item) && !isNaN(num) && item >=0 && item < 256 && arr.length==4 && test;
  }
}
  • Note fails if alphabetical char appears in a part as ```parseInt("2a00", 10)``` returns ```2``` and not ```NaN```, so an ip of ```200.200.2a00.200``` ends up being accepted as valid when it's not. – Adam Parkin Mar 29 '18 at 20:40
  • Thanks Adam, modified code to look at both parseInt and original num by adding isNaN(num) – Dave Joyce Apr 20 '18 at 18:59
0

In addition to a solution without regex:

const checkValidIpv4 = (entry) => {
  const mainPipeline = [
    block => !isNaN(parseInt(block, 10)),
    block => parseInt(block,10) >= 0,
    block => parseInt(block,10) <= 255,
    block => String(block).length === 1
      || String(block).length > 1
      && String(block)[0] !== '0',
  ];

  const blocks = entry.split(".");
  if(blocks.length === 4
    && !blocks.every(block => parseInt(block, 10) === 0)) {
    return blocks.every(block =>
      mainPipeline.every(ckeck => ckeck(block) )
    );
  }

  return false;
}

console.log(checkValidIpv4('0.0.0.0')); //false
console.log(checkValidIpv4('0.0.0.1')); //true
console.log(checkValidIpv4('0.01.001.0')); //false
console.log(checkValidIpv4('8.0.8.0')); //true
0

This should work:

function isValidIP(str) {
  const arr = str.split(".").filter((el) => {
    return !/^0.|\D/g.test(el);
  });

  return arr.filter((el) => el.length && el >= 0 && el <= 255).length === 4;
}
Filas Siuma
  • 301
  • 1
  • 8
0

well I try this, I considered cases and how the entries had to be:

function isValidIP(str) {
   let cong= /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/  
   return cong.test(str);}
0

A regular expression for validate well conformed ip address

/^(25[0-4]|2[0-4]\d|1(\d{2})|[1-9]\d|[1-9])(\.(25[0-4]|2[0-4]\d|1(\d{2})|[1-9]\d|\d)){3}$/

Example:

const ipArr = ['\\\\115.42.150.37\\', '\\\\192.168.0.1\\', '   \\\\66.88.3', '\\\\110.234.52.124\\', '\\\\Z:\\'];

const getIpHost = (ip) => {
  if (ip) {
    const hostPortArr = ip.replace(/\\/g, '').split(':');
    return (hostPortArr[0] || '').trim();
  }
  return '';
}

const validateIp = (ip) => {
  if (!ip?.trim().length) {
    return false;
  }

  const ipAdr = getIpHost(ip);
  const regExp = /^(25[0-4]|2[0-4]\d|1(\d{2})|[1-9]\d|[1-9])(\.(25[0-4]|2[0-4]\d|1(\d{2})|[1-9]\d|\d)){3}$/;
  return regExp.test(ipAdr);
}

ipArr.forEach((ip) => {
  const isValid = validateIp(ip);
  console.log(`${ip}:`, isValid)
})

Console Output:

\\115.42.150.37\: true
\\192.168.0.1\: true
   \\66.88.3: false
\\110.234.52.124\: true
\\Z:\: false
-1

A less stringent when testing the type not the validity. For example when sorting columns use this check to see which sort to use.

export const isIpAddress = (ipAddress) => 
    /^((\d){1,3}\.){3}(\d){1,3}$/.test(ipAddress)

When checking for validity use this test. An even more stringent test checking that the IP 8-bit numbers are in the range 0-255:

export const isValidIpAddress = (ipAddress) => 
    /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipAddress)
shawfire
  • 69
  • 4