user3262732
user3262732

Reputation: 246

regular expression for a url

I want that form submit when user enter their domain without https:// or http:// or / at the end.what is the regular expression for this.

https://google.com   wrong
http://google.com    wrong
google.com           correct
subdomain.google.com correct
google.com/          wrong

Please tell me what is the regular expression for this

Upvotes: 0

Views: 67

Answers (3)

Amadan
Amadan

Reputation: 198314

What you want is a "fully qualified domain name" (FQDN), not a URL. FQDN regexp is the following (from this answer):

(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)

Simpler regular expressions can result in FQDNs that violate the RFC, by being too long, by allowing disallowed characters, or characters in disallowed positions (e.g. -.-, a..a or __.com would be matched my some of the other suggestions on this page, while not being valid FQDNs).

Upvotes: 0

Ian Hazzard
Ian Hazzard

Reputation: 7771

Instead of @vks's answer, I would do the more simple: ^[\w.]+\.[a-z]+$. This makes sure that has a word, and that there is a period "." and then a few more lowercase letters.

I hope that helps!

Upvotes: 0

vks
vks

Reputation: 67968

^(?!https?:\/\/)\w+(\.\w+)+$

You can try this.See demo.

http://regex101.com/r/oE6jJ1/41

^(?!https?:\/\/)(?!.*?\/$)[\w./]+$

You can try this is you have inputs like google.com/something.See demo.

http://regex101.com/r/oE6jJ1/40

Upvotes: 3

Related Questions