Find a regular rule to use for ip fuzzy search in the text boxUse
The correct approximate format is:
1.1
1.11.1
1.1.11
Satisfy the following filter items:
Cannot start with ".": .1.1.11
Cannot end with ".": 1.1.11.
Cannot have 2 consecutive ".": 1. .1.11
cannot contain symbols other than the English period ".": 1@1.1
cannot contain spaces: 1 .1 .1
cannot contain letters or Chinese characters
Only 1 number can pass
Because I am not very good at regular expressions, I use a stupid method to eliminate them step by step. My idea is to first remove the inner and outer spaces:
1. Remove the inner spaces: /^S*$/
2. Remove spaces on both sides:
while((value.indexOf(" ") == 0) && (value.length > 1)) {
return false;
}
while((value.lastIndexOf(" ") == value.length - 1) && (value.length > 1)) {
return false;
}
3. Then exclude symbols: /<|>|||*|?|\|"|/|&|#|@|!|~|(|)/;
4. Remove Chinese The regular rules: /^[u4e00-u9fa5] $/;
It won’t be used later, and I feel that these methods are very cumbersome and redundant. Is there any regular rules that can solve this problem? Thank you!
Among them,
(d|[1-9]d|1dd|2[0-4]d|25[0-5])
is specially used to match numbers from 0 to 255. The above regular expression can Understood as:([0~255].){3}[0~255]
To put it bluntly, it means starting with a number and ending with a number, and you can insert a period in the middle of the number.
The IP address usually has two dots, so
The IP address is in the format of x.x.x.x. The longest x is a 3-digit integer and does not exceed 255, so my regular expression is as follows:
/^([0-9]|[1-9]d|1dd|2[0-4]d|25[0-5])(.([0-9]|[1-9]d| 1dd|2[0-4]d|25[0-5])){0,3}$/
This part is mainly to match numbers between [0,255]
([0-9]|[1-9]d|1dd|2[0-4]d|25[0-5])
/^d+(?:.d+)*$/
Has been personally tested to meet the subject’s needs