search

Home  >  Q&A  >  body text

javascript - This is how to use regular expressions to verify whether the format of the server address and port number is correct. Please give me some advice!

伊谢尔伦伊谢尔伦2808 days ago568

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-05-19 10:30:54

    I didn’t see your regular expression, but I can check it roughly like this

    var reg = /^[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}$/;
    var regp = /^[\d]+$/;
    var s = ['192.168.20.1.1', '192.168.20.1.1.1', '192.168.20.1a', '192.168.20.1', '192.168.20.10000'];
    var p = [8080, 0, 333, 'a', '1+'];
    for (let i = 0, len = s.length; i < len; i++) {
      console.log('ip', s[i], reg.test(s[i]))
    }
    for (let i = 0, len = p.length; i < len; i++) {
      console.log('port', p[i], regp.test(p[i]))
    }

    reply
    0
  • Cancelreply