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
如何使用 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
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
Regexper
# 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])/;
# `$` 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])$/;
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
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp
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://stackoverflow.com/a/16321775/5934465
https://www.cnblogs.com/xgqfrms/p/17324587.html#5168455
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17351421.html
未经授权禁止转载,违者必究!