20

I'm looking for a regex that accept urls like these:

http://www.example.com
www.example.com

This is what I have so far, but it regex doesn't match URLs without http:// or https://, or ftp://:

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

How can I make the protocol optional?

5

6 Answers 6

16

Make the (ftp|http|https):\/\/ part optional:

((ftp|http|https):\/\/)?
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you but now I can match for example "example" or "[email protected]". And I don't want it. So how can I solve this? My regex: regexp = /((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; I dont't want to match "[email protected]" or "example"
[email protected] is actually a valid URL: The first example is a username, that can be submitted this way. Try loading this very page with an added example@ between http:// and stackoverflow.com.
I don't want to accept @ in my urls. Do you know how I can change my regex?
4

Try this this will validate url with (http,ftp,https) or without(http,ftp,https)..

/^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{3,6}$/;

3 Comments

This regex do not accept url for domains from germany. e.g (www.germany.de). You have to change the regex to /^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/; The 2 is important.
This is not such a great way to do this. Really all you can do is check for a .. There are a lot of new TLDs with names longer than 6 chars...
TLDs can be between 2 and (currently the longest TLD is) 24 characters long (stackoverflow.com/questions/9238640/…). /^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{2,24}$/
4

Try this this will validate url with or without(http,ftp,https) in upper and lower cases and also allows you to for numerics

/^(?:(ftp|http|https)?:\/\/)?(?:[\w-]+\.)+([a-z]|[A-Z]|[0-9]){2,6}$/gi;

Comments

3

You can use the next reg: ^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$

Here is an example: https://www.regextester.com/93652

Comments

1

Kindly see https://codegolf.stackexchange.com/a/480/6593

Quoting from the above link:

value = 'www.google.com';
if(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi.test(value)) {
            return true;
        } else {
            return false;
        }

Comments

1

regex accept url without http or https

[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@%_\+.~#?&//=]*)

In HTML5

<input pattern="[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@%_\+.~#?&//=]*)" type="text>

1 Comment

The HTML5 string givves an error that the pattern is invalid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.