/192.168.0.(12[0-8]|1[0-1][0-9]|[2-9][0-9]|1[2-9])/
this regular means that as long as there is a substring in the test string to make this regular match, then true
will be returned, so when you use '192.168.0.129'
to test, because the substring 192.168.0.12
meets the requirements, the test result is true
.
so to avoid this, add ^
at the beginning of the regular and $
at the end of the regular.
^
matches the beginning of the string, matches a position;
$
matches the end of the string, matches a position;
/^192.168.0.(12[0-8]|1[0-1][0-9]|[2-9][0-9]|1[2-9])$/
plus ^
$
means that the string should the whole
match the expression, not one of the substrings matches
. At this time, the test '192.168.0.129'
will return false
.
if your requirement is to determine whether there is a match in the substring
, you can simply add a assertion
to the end so that the following character is not a number:
/192.168.0.(12[0-8]|1[0-1][0-9]|[2-9][0-9]|1[2-9])(?![0-9])/
this requirement is recommended not to use regular implementation, just use regular rough Filter as a positive integer, and then convert it into an integer to judge the size. Anyway, js is executed at the front end, and this speed is not too slow, and it is easy to maintain. Do you think you can understand it after you write this rule?
EDIT, does not take a closer look at your requirements, which should not be realized by regularization at all, but should be done directly with ipv4's parsing class library. wouldn't it be beautiful to directly limit the ip section?
the numerical range is usually segmented regular, for example, 12-128, which is divided into: 12-19,20-99,100-119120-128: / ^ ((1 [2-9]) | ([2-9] [0-9]) | (1 [01] [0-9]) | (12 [0-8])) $/
for more complex ones, extract the numbers directly, and then judge by if/else. Simple regularities + simple judgments are better than complex regularities.