T
ToolPrime

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