What Is a Good Regex for URL Validation?
URL regex: https?:\/\/[^\s/$.?#].[^\s]*. This practical pattern matches HTTP and HTTPS URLs. It checks for a protocol (http or https), a :// separator, at least one valid character for the domain, and allows any non-whitespace characters for the rest of the URL.
Usage Examples
JavaScript
// Regex approach
const urlRegex = /https?:\/\/[^\s/$.?#].[^\s]*/g;
const text = 'Visit https://example.com/path?q=1 for more';
text.match(urlRegex);
// ["https://example.com/path?q=1"]
// Better: URL constructor (for strict validation)
function isValidURL(str) {
try { new URL(str); return true; }
catch { return false; }
}
isValidURL('https://example.com'); // true
isValidURL('not-a-url'); // false
Python
import re
pattern = r'https?://[^\s/$.?#].[^\s]*'
re.findall(pattern, 'Visit https://example.com for more')
# ['https://example.com']
# Better: urllib.parse
from urllib.parse import urlparse
result = urlparse('https://example.com/path')
bool(result.scheme and result.netloc) # True
When to Use Regex vs. URL Parser
- Regex: Extracting URLs from unstructured text, quick format checks
- URL constructor: Strict validation, production form inputs, API inputs
Try It Yourself
Test this regex with our Regex Tester.
Frequently Asked Questions
Should I use regex or the URL constructor to validate URLs?
For production code, prefer new URL(string) in JavaScript or urllib.parse in Python. Use regex for extracting URLs from unstructured content.
Does this regex handle international domain names?
This basic regex does not handle internationalized domain names (IDN) with non-ASCII characters. For full IDN support, convert to Punycode first or use a URL parser library.