URL / Website Validation Regex (HTTP & HTTPS)
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$Quick answer
This regex validates HTTP and HTTPS URLs. It ensures the URL starts with a protocol (optional), has a valid domain name, and allows subdirectories.
🛠️ Fix it right here with AI Regex Generator
Need to modify this pattern or generate a new one? Edit the regex below and let our AI help.
Live Regex Tester
Type a string below to see if it matches this pattern.
How it works
^(https?:\/\/)? optionally matches 'http://' or 'https://'. ([\da-z\.-]+) matches the domain name. \.([a-z\.]{2,6}) ensures a valid TLD. The rest allows for paths and query strings.
Code Examples
✅ Correct Usage
// JavaScript
const urlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
console.log(urlRegex.test("https://www.example.com/path")); // true❌ What NOT to do
// BAD: Rejects URLs without http:// const badRegex = /^https?:\/\//;
Frequently asked questions
Does this require http:// or https://?
No, the (https?:\/\/)? part is optional. It will match 'example.com' as well as 'https://example.com'.
Does it validate IP addresses?
No, this is for domain names. If you need to validate IPs, use a dedicated IPv4 regex.
Related Search Terms
Related Regex Patterns
Need a custom regex pattern?
Use the free to describe what you need in plain English.
Want an AI with memory for your project?
MonsterX AI remembers your codebase across chats.