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

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

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.

Related Tools