以下是我的代码我试图在前端验证IP地址,但不知何故,这个正则表达式无法正常工作 -
var patt = /[0-255]\.[0-255]\.[0-255]\.[0-255]/;
var str = "90.89.99.90";
if(str.match(patt))
console.log("Valid IP");
else
console.log("Invalid IP");
应输出 - 有效IP
虽然这些IP应该返回 - 无效的IP
" fd.45.67.90"," 256.67.67.89"," @@。45.67.90"等等
让我知道我在使用正则表达式做错了什么。
答案 0 :(得分:4)
正则表达式范围不符合您的想法。 [x-y]
范围包含从x
到y
的ascii中的所有字符。
因此[0-255]
匹配0
到2
(又名0
,1
和2
)和5
或简而言之 - [0125]
。
要匹配0
到255
之间的数字,您可以执行以下操作:
\d\d?|1\d\d|2([0-4]\d|5[0-5])
这个想法:
\d\d?
- 一位或两位数字1\d\d
- 从100
到199
2[0-4]\d
- 从200
到249
的数字。25[0-5]
- 从250
到255
要匹配整个IP,您可以这样做:
^((\d\d?|1\d\d|2([0-4]\d|5[0-5]))\.){3}(\d\d?|1\d\d|2([0-4]\d|5[0-5]))$
答案 1 :(得分:3)
[...]
表示"字符类"意思是它列出了应该匹配的字符;它匹配您列出的任何单个字符。所以你的正则表达式是检查每个段的单个字符,它是0,1,2或5中的任何一个(0-2
给我们0,1和2;然后5给我们5 ,第二个被忽略了)。它应检查数字(\d
)及其中的1-3({1,3}
):
var patt = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
您可能还需要^
(输入开始)和$
(输入结束),以便foo123.123.123.123bar
不匹配:
var patt = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/$;
当然,这不足以检查IP 有效。它只是检查有预期的数字;它会很高兴地称891.262.999.42
有效。
如果要正确验证它(例如,数字在有效范围内,过滤掉无效的段组合等),您可以提取this comprehensive URL validation regex的IP部分(因为URL可以包含IP)地址,它涵盖了)。值得庆幸的是,迭戈和其他作者很好地评论了它,清楚地确定哪些位做了什么:
//
// Regular Expression for URL validation
//
// Author: Diego Perini
// Updated: 2010/12/05
// License: MIT
//
// Copyright (c) 2010-2013 Diego Perini (http://www.iport.it)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// the regular expression composed & commented
// could be easily tweaked for RFC compliance,
// it was expressly modified to fit & satisfy
// these test for an URL shortener:
//
// http://mathiasbynens.be/demo/url-regex
//
// Notes on possible differences from a standard/generic validation:
//
// - utf-8 char class take in consideration the full Unicode range
// - TLDs have been made mandatory so single names like "localhost" fails
// - protocols have been restricted to ftp, http and https only as requested
//
// Changes:
//
// - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
// first and last IP address of each class is considered invalid
// (since they are broadcast/network addresses)
//
// - Added exclusion of private, reserved and/or local networks ranges
//
// - Made starting path slash optional (http://example.com?foo=bar)
//
// - Allow a dot (.) at the end of hostnames (http://example.com.)
//
// Compressed one-line versions:
//
// Javascript version
//
// /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i
//
// PHP version
//
// _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$_iuS
//
var re_weburl = new RegExp(
"^" +
// protocol identifier
"(?:(?:https?|ftp)://)" +
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?" +
"(?:" +
// IP address exclusion
// private & local networks
"(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
"|" +
// host name
"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
// domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
// TLD identifier
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
// TLD may end with dot
"\\.?" +
")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:[/?#]\\S*)?" +
"$", "i"
);
答案 2 :(得分:1)
这是一个正则表达式变体(基于ndn' s answer above),它将检查整个输入文本是否为有效的IP:
^(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))$
请参阅regex demo
以下是一个解决方案,可用于从较大的文本中提取有效的IP:
\b(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\b
请参阅another demo
解释:
^
- 字符串的开头(如果您不需要在字符串的开头匹配,请替换为\b
字边界)(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}
- 3次出现
(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))
- 1位或2位数字序列,或1xx
数字(1\d{2}
)或200-255范围内的整数范围(感谢{{ 1}})2(?:[0-4]\d|5[0-5])
- 文字句号\.
- 见上文(只是IP地址的最后一部分)(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))
- 字符串结尾(如果您只需要一个完整的单词匹配,请替换为$
。)注意:在JS中,您可以使用文字表示法来声明这些正则表达式,例如:
\b