How to check URL?

I want to check the urls. He must accept:

http://google.com http://www.google.com www.google.com google.com 

I refer to Regex for matching URLs .

But it does not support google.com .

+4
source share
3 answers

Just add http:// if it is not, then check.

 if (inputURL.substring(0,7) != 'http://' && inputURL.substring(0,8) != 'https://') { inputURL = 'http://' + inputURL; } 

No large libraries are required, or anything else, just a few lines of code.

+3
source
 function isUrl(s) { var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ return regexp.test(s); } 
0
source

This is the best way that I think should be used to check 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 misunderstood.

0
source

Source: https://habr.com/ru/post/1388357/


All Articles