Skip to content
Regex for URL Validation — Pattern Explained with Examples

Regex for URL Validation — Pattern Explained with Examples

DodaTech Updated Jun 20, 2026 2 min read

URL validation is essential wherever users input web addresses — social media profiles, link sharing, website fields, and CMS systems. This pattern checks for the standard URL structure including protocol, domain, optional path, query parameters, and fragment identifiers.

The Pattern

/^https?:\/\/([\w-]+\.)+[\w-]+(\/[\w\-./?%&=]*)?$/

Pattern Breakdown

PartMeaning
^Start-of-string anchor
https?http or https protocol
:\/\/Literal colon and double slashes
([\w-]+\.)+Subdomain groups: one or more word/hyphen segments followed by a dot
[\w-]+Domain name (second-level)
(\/[\w\-./?%&=]*)?Optional path including slashes, dots, query params, and fragments
$End-of-string anchor

Matches

Does NOT Match

  • example.com (missing protocol)
  • http:///path (missing domain)
  • ftp://server (wrong protocol prefix)
  • https:// example.com (space in URL)
  • http://.com (no domain name)
  • https://exa_mple.com/invalid space (space in path)

Language Examples

JavaScript

const urlRegex = /^https?:\/\/([\w-]+\.)+[\w-]+(\/[\w\-./?%&=]*)?$/;
console.log(urlRegex.test('https://example.com'));          // true
console.log(urlRegex.test('example.com'));                   // false

Python

import re
pattern = r'^https?:\/\/([\w-]+\.)+[\w-]+(\/[\w\-./?%&=]*)?$'
print(bool(re.match(pattern, 'https://example.com')))   # True
print(bool(re.match(pattern, 'example.com')))            # False

Java

import java.util.regex.Pattern;

public class UrlValidator {
    private static final Pattern URL_PATTERN =
        Pattern.compile("^https?://([\\w-]+\\.)+[\\w-]+(/[\\w\\-./?%&=]*)?$");

    public static boolean isValid(String url) {
        return URL_PATTERN.matcher(url).matches();
    }
}

Common Pitfalls

  • No single regex covers every valid URL format — edge cases like authentication (user:pass@host), IPv6 literal hosts, and data URIs all need different patterns
  • Protocol-relative URLs (//example.com) are valid in web contexts but will be rejected by a pattern requiring http:// or https://
  • Internationalized domain names (IDN) use non-ASCII characters that standard \w does not match
  • Deep query strings with special characters like +, &, and = may fail depending on the escape handling

Real-World Use Cases

  • Link submission forms — validate user-submitted URLs before storing in a database or sharing on a platform
  • SEO tools — check that sitemap entries and backlink lists contain properly formatted URLs
  • API request validation — ensure incoming webhook URLs or callback parameters are well-formed before making outbound requests

FAQ

Should I use regex or URL parsers for validation?
Use a built-in URL parser (like JavaScript’s URL constructor or Python’s urllib.parse) for robust validation. Regex is useful for quick client-side checks, but parsers handle edge cases better.
How do I allow other protocols like ftp or mailto?
Replace https? with a protocol group like (https?|ftp|mailto): to accept multiple protocols. Be careful not to allow javascript: or other unsafe schemes in user input.

Related Patterns

Regex for Email Regex for IPv4 Address

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro