Regular expression to match DNS hostname or IP Address?

后端 未结 21 2492
长发绾君心
长发绾君心 2020-11-21 07:25

Does anyone have a regular expression handy that will match any legal DNS hostname or IP address?

It\'s easy to write one that works 95% of the time, but I\'m hoping

21条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 07:43

    AddressRegex = "^(ftp|http|https):\/\/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5})$";
    
    HostnameRegex =  /^(ftp|http|https):\/\/([a-z0-9]+\.)?[a-z0-9][a-z0-9-]*((\.[a-z]{2,6})|(\.[a-z]{2,6})(\.[a-z]{2,6}))$/i
    

    this re are used only for for this type validation

    work only if http://www.kk.com http://www.kk.co.in

    not works for

    http://www.kk.com/ http://www.kk.co.in.kk

    http://www.kk.com/dfas http://www.kk.co.in/

提交回复
热议问题