1

I have a regular expression like below in place to validate urls.

/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/)?$/

The expression successfully validate below urls.

  1. http://www.google.com
  2. http://google.com
  3. https://www.google.com
  4. https://google.com

Now i have a requirement to allow sub directory in urls which can allow n-level of sub directory. Like

http://www.google.com/dir1/dir2/...

How can i allow sub directory in above regular expression?

Thanks in advance.

J-D
  • 1,575
  • 1
  • 11
  • 22

1 Answers1

0

Try this:

http(s)?:\/\/([\w-]+\.)+[\w-]+(\/)?[.a-z0-9/-]+

Also you can check this related thread in which Matthew O'Riordan gives the regex to validate a url:

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331