RegExp Compliance Content

I am trying to convert the following from Python to node.js. This is a simple program that uses a regular expression to check if the IP address is public or private:

import re def is_private_ip(ip): """ Returns `True` if the `ip` parameter is a private network address. """ c = re.compile('(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)') if c.match(ip): return True return False print is_private_ip('192.168.0.1') # True print is_private_ip('8.8.8.8') # False print is_private_ip('109.231.231.221') # False 

I implemented it in Javascript as follows:

 var localIp = new RegExp(/(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/); console.log('192.168.0.1'.match(localIp)); console.log('8.8.8.8'.match(localIp)); console.log('109.231.231.221'.match(localIp)); 

Which gives me the following result:

 [ '192.168.', undefined, undefined, undefined, undefined, undefined, '192.168.', index: 0, input: '192.168.0.1' ] null null 

It seems to me that it works (not even sure tbh). The two IP addresses that should be publicly return null , so I assume this is correct. However, I do not understand the result of another match? I could not find out what this means.

+5
source share
5 answers
 var localIp = new RegExp(/(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/); 

console.log ('192.168.0.1'.match (localIp));

outputs the result:

 [ '192.168.', undefined, undefined, undefined, undefined, undefined, '192.168.'] 

It means:

  • '192.168.' , that is, a regular expression match on this line. only
  • undefined is a match for the first group in your regular expression: (^127\.0\.0\.1)
  • undefined for the group: (^10\.)
  • undefined for the group: (^172\.1[6-9]\.)
  • undefined for the group: (^172\.2[0-9]\.)
  • undefined for the group: (^172\.3[0-1]\.)
  • '192.168.' for group: (^192\.168\.)

thats because of the parenthesis, each of which gives a match (or undefined) plus a match returned by the match() function.

+1
source

.match () gives you the number of matches in your string. You may be looking for the .test () method.

You need to change the code as follows:

 var localIp = new RegExp(/(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/); console.log(localIp.test('192.168.0.1')); console.log(localIp.test('8.8.8.8')); console.log(localIp.test('109.231.231.221')); 

More information on the matching method can be found here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match

+1
source

String.prototype.match () :

If the regular expression does not contain the g flag, returns the same result as RegExp.exec (). The returned array has an additional input property that contains the original string that was parsed. In addition, it has an index property that represents the zero index of a match in a string.

RegExp.prototype.exec () :

The returned array has the matched text as the first element, and then one element for each parenthesis for the record, which corresponds to the content of the text that was captured.

If the match fails, the exec () method returns null.

You might want to replace RegExp.prototype.test () instead:

 var localIp = new RegExp(/(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/); console.log(localIp.test('192.168.0.1')); // => true console.log(localIp.test('8.8.8.8')); // => false console.log(localIp.test('109.231.231.221')); // => false 
+1
source

You are using the String.prototype.match method. According to the documentation, it returns "An Array containing matching results, or null if there were no matches."

In Javascript, a Array is true and null is false. This means that the following check will really correctly check if the string is a local IP:

 if(someIpString.match(localIp)) { // it is a local IP } else { // it is not a local IP } 

What you see in the array are the different parts of the original string that were matched by the corresponding groups in the regular expression. null values ​​are matches for groups you don't have, which you have a lot.

But I think you can go further. If you just want to check if a string matches a regular expression, I would recommend RegExp.prototype.test . This method returns a boolean value ( true|false ), so you do not need to rely on truth or falsification:

 if(localIp.test(someIpString)) { // it is a local IP } else { // it is not a local IP } 
+1
source

You do not need to use match groups if you do not want to capture the corresponding part of the IP address, but this is not necessary in your case. In Javascript, you can use this regex pattern (do not mark match groups):

 var localIp = new RegExp(/^127\.0\.0\.1|^10\.|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.|^192\.168\./); 

And then use it like this:

 console.log('192.168.0.1'.match(localIp) != null); console.log('8.8.8.8'.match(localIp) != null); console.log('109.231.231.221'.match(localIp) != null); 

Or, even better, use RegEx.test() :

 console.log(localIp.test('192.168.0.1')); 

Similarly, for Python, matching groups are not required.

Another thing worth noting is that your template will match invalid IPs, for example. 10.bad.ip.address will be detected as a private IP address. Not a problem if you check the IP addresses elsewhere in your application, but you can tighten it.

+1
source

Source: https://habr.com/ru/post/1240609/


All Articles