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
The i flag makes the pattern case-insensitive.
✓ 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}$/
Frequently Asked Questions
What is the regex for URL?▾
The regex pattern for URL is /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/i. Validates HTTP and HTTPS URLs with optional www prefix, domain, and path.
How do I use the URL regex in JavaScript?▾
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the URL pattern match?▾
It matches strings like https://example.com; it rejects strings like ftp://example.com. See the Examples section above for the full list of matching and non-matching cases.