首页 > 其他分享 >How use the RegExp to filter IP address in js All In One

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

时间:2023-04-25 15:44:07浏览次数:40  
标签:25 use false IP 192.168 js result test reg

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

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

reg = /192\.168\.(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[2-9])/;

reg.test(`192.168.0`);
false
reg.test(`192.168.1`);
false
reg.test(`192.168.2`);
true
reg.test(`192.168.254`);
true
reg.test(`192.168.255`);
true

image

solution

$ for end ✅

reg = /192\.168\.(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[2-9])$/;

reg.test(`192.168.0`);
false
reg.test(`192.168.1`);
false
reg.test(`192.168.2`);
true
reg.test(`192.168.254`);
true
reg.test(`192.168.255`);
true

image

Regexper

https://regexper.com/

#  no end ❌
reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])/;

image


# `$` end ✅
reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$/;

image

demos

function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-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
192.168.18.1 ❌ 1 false
192.168.18.255 ❌ 255 false

image

(

标签:25,use,false,IP,192.168,js,result,test,reg
From: https://www.cnblogs.com/xgqfrms/p/17351421.html

相关文章

  • D365: 生成Warehouse mobile app连接环境二维码
     //JSON文件格式{"ConnectionList":[{"ActiveDirectoryClientAppId":"206a394e-0dd6-44d5-a50b-796cef0d4bXX","ConnectionName":"Vyung-Test","ActiveDirectoryResource":"https://handvyung-test.......
  • 王者荣耀甄姬英雄技能中的英语单词-thrice,impact,flip,pond,四个单词就能解释这个英
    王者荣耀最近凑到100个英雄了,我最常用的法师是甄姬。1、凝泪成冰:被动:每次技能伤害都会为目标叠加印记,当印记叠满三层时目标将会被冰冻并造成350(+52%法术加成)点法术伤害(该效果5秒内对同一目标只会生效一次)。2、泪如泉涌:甄姬召唤水柱冲出地面,对接触的敌人造成500/590/680/770/86......
  • JS中的继承
    使用classextends关键字js帮我们生成的继承关系图如果我们想要实现上面的继承关系,要注意以下问题:子类和父类的属性都在1中,怎么才能实现?子类的constructor属性指向子类,怎么才能实现?子类的原型方法和父类的原型方法,各自独立,分别在2和3中,怎么才能实现?解决方法子类和父类......
  • JS中的文件流
    创建BlobnewBlob();Blob转化为URLwindow.URL.createObjectURL(blob);//DomStringwindow.URL.revokeObjectURL(objectURL);//释放怎么读取Blob文件newFileReader(blob).readAsDataURL();//Base64格式,imgsrc可以直接展示.readAsText();//字符串格式.readAsA......
  • 借灰姑娘的手,讲述js加密的美丽
    这个故事的主角是灰姑娘,她有一个重要的秘密,需要将其保护起来。但是,她发现她的网站上的JavaScript代码很容易被其他人阅读和修改,为了保护这个秘密,她需要采用一些混淆和加密技术。以下是她使用的一些技术:1.函数调用混淆灰姑娘混淆了函数的调用方式,从而使得代码难以被阅读和理解。例......
  • ERROR 1820 (HY000): You must reset your password using ALTER USER statement befo
    场景安装后首次运行mysql命令报错误:ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstatementbeforeexecutingthisstatement修改密码mysql>alteruseruser() identifiedby‘admin@123456’;QueryOK,0rowsaffected(0.03sec)mysql>flushp......
  • js空值判断
    js空值判断参考:https://blog.csdn.net/yun_master/article/details/115015113参考:https://blog.csdn.net/szl199107101035/article/details/123839403......
  • DDP运行报错(单卡无错):ERROR:torch.distributed.elastic.multiprocessing.api:failed (e
    使用DDP时出现错误,但是单卡跑无错误。错误记录如下:RuntimeError:Expectedtohavefinishedreductionintheprioriterationbeforestartinganewone.Thiserrorindicatesthatyourmodulehasparametersthatwerenotusedinproducingloss.Youcanenableunu......
  • 前端使用CryptoJS加密解密
    1、安装crypto-js;npminstallcrypto-js--save-devyarnaddcrypto-js--dev2、新建unit.js写成公共方法;constCryptoJS=require('crypto-js');//16位十六进制数作为密钥(秘钥为随机生成,必须与后端保持一致!)constkey=CryptoJS.enc.Utf8.parse("xxxxxxxxxxxxxx");//......
  • C# 启动UseShellExecute属性设置
    一个小小的细节难以不会改变大局,但多个细节可能会影响成败。在C#中使用Process启动时,ProcessStartInfo参数的UseShellExecute属性可设置为true和false,两者有什么区别,该如何使用?Process.Start本质上是启动一个新的子进程,当设置为true时,调用ShellExecute方法,设置为false时,调用Cr......