How use the RegExp to filter IP address in js All In One

发布时间 2023-04-25 00:57:09作者: xgqfrms

How use the RegExp to filter IP address in js All In One

如何使用 RegExp 在 js 中过滤 IP 地址

192.168.18.1 < 192.168.18.N < 192.168.18.255

ignore IPs: 192.168.18.0 / 192.168.18.1 / 192.168.18.255

https://regexper.com/

error

function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]|2[0-5][0-5])\.(1?[0-9]?[1-9]|2[0-4][0-9]|25[0-4])/;
   for (let i = 0; i < n; i++) {
     let result = reg.test(`192.168.18.${i}`);
     if(result) {
       // console.log(`192.168.18.${i} ✅`, i, result)
     } else {
       console.log(`192.168.18.${i} ❌`, i, result)
     }
   }
}

test(256);
// 192.168.18.0 ❌ 0 false

solution

function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]|2[0-5][0-5])\.(1?[0-9]?[1-9]|2[0-4][0-9]|25[0-4])/;
   for (let i = 0; i < n; i++) {
     let result = reg.test(`192.168.18.${i}`);
     if(result) {
       // console.log(`192.168.18.${i} ✅`, i, result)
     } else {
       console.log(`192.168.18.${i} ❌`, i, result)
     }
   }
}

test(256);

demos

(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

shell script

How to use Linux shell command filter the IP address All In One

如何使用 Linux shell 命令过滤 IP 地址

https://www.cnblogs.com/xgqfrms/p/17324587.html

refs

https://www.cnblogs.com/xgqfrms/p/17324587.html#5168455



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!