-1

I am using the below regex of validating the website URL.

^(http(s?):\/\/)?(www\.)+[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$

It work fine with the below website URL to match:


www.google.com

http://www.google.com/

https://www.google.com/


It also not math below URL


google.com

google.co

www.g@oogle.com


But it will fails to test the below URL:

www...google.com

http://www...google.com/

https://www...google.com/

Please give the suggestion for the same. I have already go through the below stack overflow URL but answer is not useful for me.

Regular expression for checking website url

What is a good regular expression to match a URL?

Community
  • 1
  • 1
Ankit Jain
  • 45
  • 1
  • 2
  • 13
  • What do you mean by "it will fails to test the below URL"? – arkascha Nov 21 '16 at 10:52
  • Of all the _strings_ you posted as examples only `http://www.google.com/` and `https://www.google.com/` are actually valid URLs. – arkascha Nov 21 '16 at 10:53
  • 1
    url validation is a complex topic. This guy created a nice comparison, check it out: https://mathiasbynens.be/demo/url-regex – Martin Gottweis Nov 21 '16 at 10:53
  • Please put your question in this format: 1. The strings? that you have. 2. The pattern you want to match in the given strings?. 3. What have you tried. – Mohammad Yusuf Nov 21 '16 at 10:54

1 Answers1

2

To avoid the ... you can use a negative lookahead

For example :

^(?!.*\.\.)(https?:\/\/)?www\.[\w.\-]+(\.[a-zA-Z]{2,3})+(\/[\w.?%#&=\/\-]*)?$

The (?!.*\.\.) in that regex won't allow 2 dots in the string.

LukStorms
  • 28,916
  • 5
  • 31
  • 45