How to validate url?

后端 未结 4 573
自闭症患者
自闭症患者 2020-12-21 06:11

I want to validate urls. It should accept:

http://google.com

http://www.google.com

www.google.com

google.com

I refer Regex to match URL

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-21 06:22

    This is a better way I think should be used to validate urls

    reg = /https?:\/\/w{0,3}\w*?\.(\w*?\.)?\w{2,3}\S*|www\.(\w*?\.)?\w*?\.\w{2,3}\S*|(\w*?\.)?\w*?\.\w{2,3}[\/\?]\S*/
    reg.test('www.google.com')    # will return true
    reg.test('www.google')        # will return false
    

    Let me know if you still not getting it correct.

提交回复
热议问题