🔍Regex Recipes

URL Validation (HTTP/HTTPS)

Validate HTTP and HTTPS URLs including query strings and fragments.

Pattern

^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)$

Explanation

Matches common URL formats with optional www, query parameters, and fragments.

Examples

Basic HTTPS
Input
https://example.com
Output
✓ Match
With path
Input
https://example.com/path/to/page
Output
✓ Match
With query
Input
https://example.com?param=value&foo=bar
Output
✓ Match
With fragment
Input
https://example.com/page#section
Output
✓ Match
Invalid - no protocol
Input
example.com
Output
✗ No match

Code Examples

JavaScript
const urlRegex = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)$/;
const isValidUrl = urlRegex.test('https://example.com'); // true

Try it Now

💡 Tips

  • Consider using URL parsing libraries for production use
  • Test with and without www prefix
  • Remember to handle URL encoding for special characters

⚠️ Common Pitfalls

  • Does not validate if URL is actually accessible
  • May not match all valid URL formats per RFC 3986
  • International domain names need special handling