search

Home  >  Q&A  >  body text

javascript - Looking for a set of JS regular rules about IP addresses!

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]/
扔个三星炸死你扔个三星炸死你2793 days ago824

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-07-05 10:40:27


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

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 10:40:27

    ^((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])|)$

    reply
    0
  • 漂亮男人

    漂亮男人2017-07-05 10:40:27

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

    reply
    0
  • 为情所困

    为情所困2017-07-05 10:40:27

    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

    reply
    0
  • Cancelreply