Back to Home

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!

Generate Regex Pattern

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

ComponentDescriptionMatches
^Start anchorEnsures match from string start
(https?:\/\/)?Optional protocolOptional http:// or https://
([\da-z\.-]+)Domain nameOne or more: digits, lowercase letters, dots, hyphens
\.TLD separatorLiteral dot
([a-z\.]{2,6})TLD2-6 lowercase letters or dots
([\/\w \.-]*)*Optional pathZero or more path segments
\/?Optional trailing slashOptional forward slash
$End anchorEnsures match to string end

Character Classes

  • https? - http or https (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.com
  • http://www.example.com
  • example.com
  • subdomain.example.org
  • example.co.uk
  • example.com/path/to/page
  • example.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

  1. Lowercase only: Only accepts lowercase letters in domain and TLD
  2. No port numbers: Doesn't support :8080 port specification
  3. No query strings: Doesn't validate query parameters (?key=value)
  4. No fragments: Doesn't support URL fragments (#section)
  5. No IPv4/IPv6: Doesn't validate IP addresses as hosts
  6. 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!

Generate Regex Pattern