[英]RegEx Javascript - What am I doing wrong?
I have this pattern stored in a variable: 我将此模式存储在变量中:
var regexServer = /^([0-9]{2,3})+\.([0-9]{2,3})+\.([0-9]{2,3})+\.([0-9]{2,3})+\:[0-9]{2,4}$/;
if(!stringFromArgument.match(regexServer))
alert("You must input a valid IP and a Port address! Eg: 66.77.88.99:8000");
I checked the string coming from the input (form), and it's ok. 我检查了来自输入(窗体)的字符串,没关系。
If somebody wants to add his own IP and Port address he must add a valid form. 如果有人要添加自己的IP和端口地址,则必须添加有效的表格。 His string must contain only
.
他的弦只能包含
.
, :
, numbers and no whitespace. ,
:
,数字,没有空格。
What am I doing wrong? 我究竟做错了什么?
Whats your problem? 你怎么了?
I see two things 我看到两件事
/^([0-9]{2,3})+\.([0-9]{2,3})+\.([0-9]{2,3})+\.([0-9]{2,3})+\:[0-9]{2,4}$/
^ ^ ^ ^
^
您接受的IP编号从10到255,也许您应该将量词从{2,3}更改为{1,3},否则IP地址(例如128.0.0.1:80)将被拒绝。
I think your RegExp isn't working because the +
characters. 我认为您的RegExp无法正常工作,因为
+
字符。
This should work and is in my opinion a more efficient regular expression: /^([0-9]{1,3}(\\.?)){4}:[0-9]{2,5}$/
这应该可行,并且在我看来是更有效的正则表达式:/
/^([0-9]{1,3}(\\.?)){4}:[0-9]{2,5}$/
:[ /^([0-9]{1,3}(\\.?)){4}:[0-9]{2,5}$/
You should allow 1 number for each level. 您应该为每个级别允许一个数字。 For example:
82.176.103.6
wouldn't match if you didn't. 例如:如果您不匹配,则不会匹配
82.176.103.6
。 Also, the amount of numbers for ports should be 5 because the highest port number is 65535
另外,端口号的数量应为5,因为最高端口号为
65535
First, no need put (…)+
if you just testing. 首先,如果您只是进行测试,则无需放置
(…)+
。 Also you've limited 3rd and 4th numbers to 2+, but there could be numbers lower than 10 as well as the port could be greater than 9999. 另外,您已将第3和第4个数字限制为2+,但是数字可能小于10,端口也可能大于9999。
var regexServer = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{2,4}$/
Also, this regex only checks if correct format, but not validating the ip (checks if the numbers is between 0-255). 另外,此正则表达式仅检查格式是否正确,而不验证ip(检查数字是否在0-255之间)。 Here is some more regex (without the port check, you can add it by your self):
这是更多的正则表达式(无需端口检查,您可以自行添加):
var regexServer = /^(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9])[.]){3}(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9]))$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.