Regular expression for validating URL in Javascript

I was looking for a decent regular expression to validate URLs that were entered as user input with.
After lots of searching on google i got the perfect solution which satisfy my requirement of my current assignment
Following is the regular expression for validating URL (Valid URL Regular Expression) a have used which worked perfectly.

^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$

Following is the pattern results for the same
  • https://www.test.com   -Passed
  • http://www.test.com   -Passed
  • www.test.com   -Passed
  • test.com   -Passed
  • http://blog.test.com   -Passed
  • http://www.test.com/product   -Passed
  • http://www.test.com/products?id=1&page=2   -Passed
  • http://www.test.com#up   -Passed
  • http://255.255.255.255   -Passed
  • 255.255.255.255   -Passed
  • http://invalid.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2   -Failed
  • http://www.site.com:8008   -Passed

Comment if you like it!!

Comments