URL & Path Validation Regular Expression
A valid URL (Uniform Resource Locator) follows the format defined in RFC 3986. This regex validates HTTP and HTTPS URLs with optional www prefix, domain names, paths, query strings, and fragments. URLs are essential for web navigation and API endpoints.
Recommended Solution
Explanation
^- Start of the string.https?- Protocol: http or https.:\/\/- Literal "://" separator.(?:www\.)?- Optional "www." prefix.[-a-zA-Z0-9@:%._\+~#=]{1,256}- Domain name or subdomain (1-256 characters). See domain validation for more details.\.[a-zA-Z0-9()]{1,6}- Top-level domain (e.g., .com, .org) with 1-6 characters.\b- Word boundary to ensure proper domain ending.(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)- Optional path, query string, and fragment.$- End of the string.
Notes
- This regex validates the format of URLs but does NOT verify if the URL is accessible or exists.
- This pattern only supports HTTP and HTTPS protocols. To support other protocols (ftp, mailto, etc.), modify the protocol part.
- Port numbers are implicitly allowed through the character class but not explicitly validated.
- IP addresses (IPv4/IPv6) are not fully supported by this pattern. Use a dedicated IP address validation regex if needed.
- International domain names (IDN) must be in Punycode format to be validated correctly.
- Query parameters and fragments are allowed but not individually validated for correctness.
- For more robust validation, consider using built-in URL parsing libraries in your programming language.
Implementation
Test Cases
| URL | Valid |
|---|---|
| https://www.example.com | |
| http://example.com | |
| https://example.com/path/to/resource | |
| https://example.com/path?query=value | |
| https://example.com:8080/path | |
| https://subdomain.example.com | |
| http://example.com/path#fragment | |
| https://example.com/path?key1=value1&key2=value2 | |
| https://example.co.uk | |
| https://192.168.1.1 | |
| ftp://example.com | |
| https:// | |
| example.com | |
| www.example.com | |
| https://example | |
| https://example. | |
| https://ex ample.com | |
| (empty string) | |
| http:/example.com | |
| https//example.com |