Regex for website or url validation [duplicate]

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:

// & 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):\/\/[^ "]+$

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.


  • ^ 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.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-%]+&?)?$/;>console.log(x+" => "+re.test(x)));

You can try this:


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 = `

let m;

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

    if (m.index === regex.lastIndex) {
    console.log("matched :"+m[0]);

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   returning with &&

Similar Posts