How to check if a URL is valid

后端 未结 9 1166
自闭症患者
自闭症患者 2020-11-28 04:23

How can I check if a string is a valid URL?

For example:

http://hello.it => yes
http:||bra.ziz, => no

If this is a valid URL

9条回答
  •  误落风尘
    2020-11-28 04:36

    In general,

    /^#{URI::regexp}$/
    

    will work well, but if you only want to match http or https, you can pass those in as options to the method:

    /^#{URI::regexp(%w(http https))}$/
    

    That tends to work a little better, if you want to reject protocols like ftp://.

提交回复
热议问题