30

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):\/\/[^ "]+$
10
  • Do you want validation for protocol or domain address?, If it's for protocol, check for default browser protocol if not specified, accordingly do validation. Commented Mar 6, 2017 at 6:30
  • Actually both, user can enter with protocol or only domain but not just any random string like, it should match those given criteria I have entered in question Commented Mar 6, 2017 at 6:32
  • 1
    so with proper protocol, if the url contains "sample.com" it should pass, is that what you mean ? Commented Mar 6, 2017 at 6:50
  • @Rizwan, Yes thats one of the best and easiest way to handle above validation. Commented Mar 6, 2017 at 6:56
  • @RizwanM.Tuman also its possible that there will be no protocol, so that should pass as well if there is no protocol I will handle it myself, why to ask user to enter it, some users even don't know HTTP or HTTPS Commented Mar 6, 2017 at 7:24

2 Answers 2

57

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)));

Sign up to request clarification or add additional context in comments.

16 Comments

added support for sample.com#
this does not support. www.google.co.in
Hi, To support URLs like https://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149 , i think you should change (#?\/?[a-zA-Z0-9#]+) to (#?\/?[a-zA-Z0-9-_#]+)
This does not support URL like "web-site.com" or "web-site.com"
Doesn't support http://blog.example.com
|
15

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]);
}

9 Comments

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
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
yeah so it could be anything, it could contain any top level domain, any domain name (not only sample), protocol, query strings & hash
:D :D I thought you were only talking about sample.com in the first place , plz take a look now :P
@DheerajAgrawal does this solve your problem now ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.