How do you regularly match to 12-128, but not to the first two digits 12 of 129?

when trying to use regular matching 12-128, it is found that it can match to 129, such as the question.

/ 192.168.0.(12[0-8]|1[0-1][0-9]|[2-9][0-9]|1[2-9]) /
True is returned when

matches 129.
judgment 255 will also match the first two digits 25.

how can this be avoided?

or is it written in some other way?


/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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9a148-17ae.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9a148-17ae.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?