javascript - Looking for a set of JS regular rules about IP addresses!
扔个三星炸死你
扔个三星炸死你 2017-07-05 10:38:27
0
4
819

For example, if you want to match this

22.22.22.0/16
33.33.33.1/24

How to write such a regular change? Please give me some advice


This seems to work

/((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)\/[0-2][0-4]/
扔个三星炸死你
扔个三星炸死你

reply all(4)
洪涛


Given in "Must Know Regular Expressions". It’s easy to make mistakes when you write it yourself

过去多啦不再A梦

^((0|1[0-9]{0,2}|2[0-9]{0,1}|20-4|25[0-5]|3-9{0,1}).){3}(0|1[0-9]{0,2}|2[0-9]{0,1}|20-4|25[0-5]|3-9{0,1})(?(/)/([0-9]|1-2|3[0-2])|)$

漂亮男人

new RegExp(/([0-9]{1,3}.{1}){3}[0-9]{1,3}/)

为情所困

You can directly find a regular match for IP on the Internet, but the one on the Internet may not be correct, and it is not easy to verify, so we will write one ourselves.

There is a tool for generating regular expressions from a numerical range. [Tools can be found online]
ip rules
0-255.0-255.0-255.0-255/0-32

0-255

([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25 [0-5])

0-255.

([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25 [0-5]).

Repeat 3 times

(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]| 25[0-5]).){3}

Splice again 0-255

(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]| 25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0 -4][0-9]|25[0-5])

0-32

([0-9]|[12][0-9]|3[0-2])

All stitched together

(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]| 25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0 -4][0-9]|25[0-5])/([0-9]|[12][0-9]|3[0-2])

Add first separator

^(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9] |25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[ 0-4][0-9]|25[0-5])/([0-9]|[12][0-9]|3[0-2])$
or
(?^ |s)(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9] |25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[ 0-4][0-9]|25[0-5])/([0-9]|[12][0-9]|3[0-2])(?:s|$)

  • The dividing symbols are replaced according to the actual situation

  • If no separator is added, 33.33.33.1/24 will be matched into 33.33.33.1/2 or 99933.33.33.1/24 will also be matched

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template