'Is there any url regex validator that fits this criteria?
I need a url validator regex with this criteria:
- protocol (HTTP, HTTPS) is optional. But if any protocol is given, it must be in the correct format, i.e. protocol:domain, or protocol://domain.
- www is optional
- it's possible to use direct IP address for this.
So based on the criteria, these should pass:
- http://www.google.com
- google.com
- abc.def.ghi/hij
- https:216.239.38.120
- 216.239.38.120
These should not pass:
- hello
- hello/world
- abc://def.ghi
- ftp:google.com
The closest regex I've found is from here:
^((?:.|\n)*?)((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)([-A-Z0-9.]+)(/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#/%=~_|!:,.;]*)?)
But unfortunately, google.com doesn't pass. It needs to have www. as a prefix. Can you improve this regex so www. becomes optional?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
