Regex for URL
Validates HTTP and HTTPS URLs with optional www prefix, domain, and path.
Pattern
/^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/i Live Tester
Enter a string to test
Examples
✓ https://example.com
✓ http://www.test.org/path?q=1
✓ https://sub.domain.co.uk/page
✗ ftp://example.com
✗ example.com
✗ http://
Pattern Breakdown
^https?:\/\/ — starts with http:// or https://
(www\.)? — optional www. prefix
[a-zA-Z0-9@:%._+~#=]{1,256} — domain name characters
\.[a-zA-Z0-9()]{1,6} — dot followed by TLD
\b([...]*) $ — optional path, query string, and fragment
Code Snippets
Javascript
const regex = /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/i;
regex.test("https://example.com"); // true Python
import re
pattern = r"^https?://(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$"
bool(re.match(pattern, "https://example.com", re.IGNORECASE)) # True Php
$pattern = '/^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&\/=-]*)$/i';
preg_match($pattern, "https://example.com"); // 1 Related Patterns
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv4 Address /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/ IPv6 Address /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/