Regex URL Pattern — Match and Validate URLs
URL validation with regex depends on what you need to match: full URLs with protocol, domain-only, or URL slugs all require different patterns. Choose the pattern below that fits your use case, or use the nativeURL constructor in JavaScript for the most reliable validation.4 URL Regex Patterns
Simple HTTP/HTTPS URL
/^https?:\/\/[^\s/$.?#].[^\s]*$/Matches
https://example.com
http://sub.domain.co/path?q=1
Does not match
ftp://example.com
not-a-url
example.com
Matches any URL starting with http:// or https://.
Full URL with optional path, query, fragment
/^(https?:\/\/)?(www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(\/[^\s]*)?$/Matches
https://example.com/path
www.domain.org
example.co.uk/page?id=1
Does not match
http://
://example
not valid url
More permissive — also matches URLs without the protocol prefix.
Domain name only
/^([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,}$/Matches
example.com
sub.domain.co.uk
my-site.io
Does not match
http://example.com
.com
example
Use when you want to match domain names without a protocol.
URL slug (path segment)
/^[a-z0-9]+(?:-[a-z0-9]+)*$/Matches
my-page
hello-world-123
slug
Does not match
My-Page
hello world
slug/path
-leading-dash
Validates URL-friendly slugs: lowercase letters, numbers, hyphens.
Code Examples
JavaScript
const urlRegex = /^https?:\/\/[^\s/$.?#].[^\s]*$/;
function isValidUrl(url) {
return urlRegex.test(url);
}
console.log(isValidUrl("https://example.com")); // true
console.log(isValidUrl("not-a-url")); // false
// Alternative: use the URL constructor
function isValidUrlNative(url) {
try {
new URL(url);
return true;
} catch {
return false;
}
}Python
import re
pattern = r'^https?:\/\/[^\s/$.?#].[^\s]*$'
def is_valid_url(url):
return bool(re.match(pattern, url))
print(is_valid_url("https://example.com")) # True
print(is_valid_url("not-a-url")) # FalseTest Your Pattern Online
Related Guides
- → Regex Email Pattern
- → Regex Password Validation
- → Regex Numbers Only Pattern
- → Common Regex Patterns
- → Regex Date Format
- → Regex Phone Number
Frequently Asked Questions
Should I use regex or the URL constructor to validate URLs?
Use the native URL constructor in JavaScript when possible — it handles edge cases better than regex. Use regex when you need to match specific URL formats like slugs or domains.
What is a URL slug?
A URL slug is the human-readable part of a URL, like 'my-page' in example.com/my-page. Slugs use lowercase letters, numbers, and hyphens only.
Can one regex match all valid URLs?
No. URLs can be extremely varied (protocols, ports, query strings, anchors, international domains). Use multiple targeted patterns for different URL types.
All tools run in your browser. Your data never leaves your device.