Ken*_*nC. 45 javascript regex string string-matching
我有几个IP地址,如:
115.42.150.37115.42.150.38115.42.150.50如果我想搜索所有3个ip地址,我应该写什么类型的正则表达式?例如,如果我这样做115.42.150.*(我将能够搜索所有3个IP地址)
我现在能做的就是:/[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/但似乎效果不好.
谢谢.
Eri*_*est 94
可能会迟到但有人可以尝试:
VALID IP地址示例
115.42.150.37
192.168.0.1
110.234.52.124
Run Code Online (Sandbox Code Playgroud)
INVALID IP地址的示例
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]
Run Code Online (Sandbox Code Playgroud)
用于验证IP地址的JavaScript代码
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)
}
Run Code Online (Sandbox Code Playgroud)
ori*_*dam 41
试试这个,这是一个较短的版本:
^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$
Run Code Online (Sandbox Code Playgroud)
解释:
^ 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
Run Code Online (Sandbox Code Playgroud)
浏览器控制台的单元测试:
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);});
Run Code Online (Sandbox Code Playgroud)
Spu*_*ley 20
你已经得到的正则表达式有几个问题:
首先,它包含点.在正则表达式中,点表示"匹配任何字符",您只需匹配实际点.为此,你需要逃脱它,所以在点前放一个反斜杠.
其次,但你在每个部分匹配任何三位数.这意味着您将匹配0到999之间的任何数字,这显然包含许多无效的IP地址编号.
这可以通过使数字匹配更复杂来解决; 在这个网站上有其他答案解释如何做到这一点,但坦率地说,这是不值得的 - 在我看来,你最好用点分割字符串,然后只是将四个块验证为数字整数范围 - 即:
if(block >= 0 && block <= 255) {....}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
小智 18
如果您正在使用nodejs尝试:
require('net').isIP('10.0.0.1')
doc net.isIP()
Tej*_*eni 15
试试这个..源于此.
"\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"
Run Code Online (Sandbox Code Playgroud)
Ben*_*ick 11
如果你想在现代浏览器中为ipv4使用比regex更具可读性的东西,你可以选择
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;
}
Run Code Online (Sandbox Code Playgroud)
一个简短的正则表达式: ^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4}$
例子
const isValidIp = value => (/^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4}$/.test(value) ? true : false);
// 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'));
// 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'));Run Code Online (Sandbox Code Playgroud)
以下解决方案不接受填充零
这是验证IP地址的最干净的方法,下面将其分解:
事实:有效的IP地址为4 octets,每个八位字节可以是一个介于0 - 255
正则表达式的细分,匹配以下任意值 0 - 255
25[0-5] 火柴 250 - 2552[0-4][0-9] 火柴 200 - 2491[0-9][0-9] 火柴 100 - 199[1-9][0-9]? 火柴 1 - 990 火柴 0const octet = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)';
Run Code Online (Sandbox Code Playgroud)
注意:使用new RegExp时应使用\\.而不是,\.因为字符串将被转义两次。
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);
}
Run Code Online (Sandbox Code Playgroud)
不要编写自己的正则表达式或复制粘贴!您可能不会涵盖所有边缘cses(IPv6,但也包括八进制IP等)。is-ip从npm 使用:
var isIp = require('is-ip');
isIp('192.168.0.1');
Run Code Online (Sandbox Code Playgroud)
将返回一个布尔值。
Downvoters:认真解释为什么使用主动维护的库比从网站复制粘贴更好?
我的版本为es6方法,对于有效的IP返回true,否则返回false
isIP(ip) {
if (typeof(ip) !== 'string')
return false;
if (!ip.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)) {
return false;
}
return ip.split('.').filter(octect => octect >= 0 && octect <= 255).length === 4;
}
Run Code Online (Sandbox Code Playgroud)