Reputation: 3222
I want to match a web address through regex which should capture http://www.google.com as well as www.google.com i.e. with and without protocol.
Upvotes: 0
Views: 9959
Reputation: 27258
Why not
/google\.com/
?
It catches http://www.google.com , www.google.com , and even google.com for free! :-)
Upvotes: 0
Reputation: 75862
Well it's going to depend on exactly what you want to capture ("FTP"? "/index.htm"?) because a general URI capture based on the RFC standard is very hard, but you could start with:
/^((https?\:\/\/)?([\w\d\-]+\.){2,}([\w\d]{2,})((\/[\w\d\-\.]+)*(\/[\w\d\-]+\.[\w\d]{3,4}(\?.*)?)?)?)$/
Complicated see?
Upvotes: 3
Reputation: 111200
Read RFC 3986. It is not just as easy as you might think it is. The job is easier if you only have a small set of URLs to parse.
Upvotes: 1