URL Validation Regex (Simple)
• CronOS Team
regexurlvalidationtutorialweb
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
URL Validation Regex (Simple)
A simple regex pattern for validating URLs with optional protocol, domain, TLD, and path components.
Pattern Breakdown
regex
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
(https?:\/\/)? | Optional protocol | Optional http:// or https:// |
([\da-z\.-]+) | Domain name | One or more: digits, lowercase letters, dots, hyphens |
\. | TLD separator | Literal dot |
([a-z\.]{2,6}) | TLD | 2-6 lowercase letters or dots |
([\/\w \.-]*)* | Optional path | Zero or more path segments |
\/? | Optional trailing slash | Optional forward slash |
$ | End anchor | Ensures match to string end |
Character Classes
https?-httporhttps(s is optional)\/- Literal forward slash (escaped)[\da-z\.-]- Domain characters: digits, lowercase letters, dots, hyphens[a-z\.]- TLD characters: lowercase letters and dots[\/\w \.-]- Path characters: forward slash, word chars, spaces, dots, hyphens?- Quantifier: zero or one occurrence (optional)+- Quantifier: one or more occurrences*- Quantifier: zero or more occurrences{2,6}- Quantifier: between 2 and 6 occurrences
Examples
Valid:
https://example.comhttp://www.example.comexample.comsubdomain.example.orgexample.co.ukexample.com/path/to/pageexample.com/path/to/page/
Invalid:
example(missing TLD)example.c(TLD too short)EXAMPLE.COM(uppercase not allowed)http://example(missing TLD)https://example..com(double dot)
Implementation
JavaScript
javascript
const urlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
urlRegex.test('https://example.com'); // true
urlRegex.test('example.com/path'); // true
urlRegex.test('http://subdomain.example.org'); // true
urlRegex.test('example'); // false (missing TLD)
Python
python
import re
url_regex = r'^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$'
bool(re.match(url_regex, 'https://example.com')) # True
bool(re.match(url_regex, 'example.com/path')) # True
bool(re.match(url_regex, 'example')) # False (missing TLD)
Go
go
urlRegex := regexp.MustCompile(`^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$`)
urlRegex.MatchString("https://example.com") // true
urlRegex.MatchString("example.com/path") // true
urlRegex.MatchString("example") // false (missing TLD)
Limitations
- Lowercase only: Only accepts lowercase letters in domain and TLD
- No port numbers: Doesn't support
:8080port specification - No query strings: Doesn't validate query parameters (
?key=value) - No fragments: Doesn't support URL fragments (
#section) - No IPv4/IPv6: Doesn't validate IP addresses as hosts
- Simple validation: Basic format check, not comprehensive URL validation
When to Use
- Simple URL format validation
- Client-side form validation
- Quick URL format checking
- When you need basic URL pattern matching
- Prototyping or early development
For production, consider:
- Using URL parsing libraries for comprehensive validation
- Supporting uppercase letters
- Adding port, query string, and fragment support
- Validating actual URL accessibility
- Using stricter patterns or validation libraries
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!