IP regular expression, IP address (IPV4) protocol regular expression correct writing method
IP address regular rule description
Value range | features | Regular writing | merge regex | merge regex | merge regex |
---|---|---|---|---|---|
0-9 | One digit, only one digit, the value is 0~9 | \d | [1-9]?\d | (1\d{2})|([1-9]?\d) | (25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))) |
10-99 | Two digits, the tens digit is 1-9, and the ones digit is 0~9 | [1-9]\d | |||
100-199 | Three digits, the highest digit is 1, the tens digit is 0-9, and the ones digit is 0-9 | 1\d{2} | -- | ||
200-249 | Three digits, the highest digit is 2, the tens digit is 0-4, and the ones digit is 0-9 | 2[0-4]\d | -- | -- | |
250-255 | Three digits, the highest digit is 2, the tens digit is 5, and the ones digit is 0-5 | 25[0-5] | -- | -- |
The IP address format can be expressed as: XXX.XXX.XXX.XXX, the value range of XXX is 0-255, the first three paragraphs plus one. Repeated three times, combined with the last paragraph to form the complete format of the IP address.
So the regular notation for an IP address is as follows:
let ipReg = /^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$/;
// 测试
ipReg.test('10.12.13.4'); // 输出为true