28

I know this is so common question & there are so many answers for this, but my problem is different, I want to accept the URL with all below formats:

http://www.sample.com
https://www.sample.com
http://www.sample.com/xyz
www.sample.com
www.sample.com/xyz/#/xyz
sample.com

// & much more ...

So here idea is, I am allowing the user to enter there website & another user can just click on the saved website & go to users website. So here it's not a good idea to restrict the user to input proper URL, they can enter any of the above formats. Here is the regex I am using right now, but that only checks if URL starts from proper protocol:

^(ftp|http|https):\/\/[^ "]+$
Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
Dheeraj Agrawal
  • 2,347
  • 11
  • 46
  • 63

2 Answers2

56

Use the regex ^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$

This is a basic one I build just now. A google search can give you more.

Here

  • ^ Should start with
  • ((https?|ftp|smtp)://)? may or maynot contain any of these protocols
  • (www.)? may or may not have www.
  • [a-z0-9]+(.[a-z]+) url and domain and also subdomain if any upto 2 levels
  • (/[a-zA-Z0-9#]+/?)*/? can contain path to files but not necessary. last may contain a /
  • $ should end there

var a=["http://www.sample.com","https://www.sample.com/","https://www.sample.com#","http://www.sample.com/xyz","http://www.sample.com/#xyz","www.sample.com","www.sample.com/xyz/#/xyz","sample.com","sample.com?name=foo","http://www.sample.com#xyz","http://www.sample.c"];
var re=/^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+(\.[a-z]{2,}){1,3}(#?\/?[a-zA-Z0-9#]+)*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/;
a.map(x=>console.log(x+" => "+re.test(x)));
Sagar V
  • 12,158
  • 7
  • 41
  • 68
14

You can try this:

^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?\/?$

Updated Demo

const regex = /^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?\/?$/gm;
const str = `http://www.sample.com
https://www.sample.com
http://www.sample.com/xyz
www.sample.com
www.sample.com/xyz/#/xyz
sample.com
www.sample.com
mofiz.com
kolim.com
www.murikhao.www.sample.com
http://murihao.www.sample.com
http://www.sample.com/xyz?abc=dkd&p=q&c=2
www.sample.gov.bd
www.sample.com.en
www.sample.vu
www.sample.u/


`;
let m;

while ((m = regex.exec(str)) !== null) {

    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    console.log("matched :"+m[0]);
}
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
  • this doesn't support *subdomain* _(I should have added that to my list)_ also it should support other top level domains `.tv, .in, .co.in` all this are valid – Dheeraj Agrawal Mar 06 '17 at 07:30
  • yeah you should have :) the base of my answer was to have sample.com, help me define the base, to have answer of your liking – Mustofa Rizwan Mar 06 '17 at 07:31
  • yeah so it could be anything, it could contain any top level domain, any domain name _(not only sample)_, protocol, query strings & hash – Dheeraj Agrawal Mar 06 '17 at 07:35
  • 1
    :D :D I thought you were only talking about sample.com in the first place , plz take a look now :P – Mustofa Rizwan Mar 06 '17 at 07:42
  • @DheerajAgrawal does this solve your problem now ? – Mustofa Rizwan Mar 06 '17 at 07:53
  • with the test cases I have asked in my question its working fine, but as I started testing more I found more test cases & my accepted answer is solving all those cases. I should I put all test cases while asking. – Dheeraj Agrawal Mar 06 '17 at 08:02
  • One of best answers here. @Sagar answer is also good but it is not validating some forms (i.e. www.i, www.jsjjs.) . But this answer is working for all those cases. – Shahrukh Anwar Jan 16 '19 at 05:00
  • It doesn't validate valid URLs if you add `/` at the end. For example: `https//www.google.com/` – smartmouse Feb 25 '19 at 15:40
  • Have you tested this regex by putting str value as "test", url validation not working – Nimesh khatri Jul 02 '20 at 09:42