Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(https?|ftp):\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$

Matches URLs starting with http://, https://, or ftp://, followed by optional www., domain name, TLD, and optional path/query/fragment.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string http://example.com
(https?|ftp) Group: matches either 'http' (with optional 's') or 'ftp' http, https, ftp
? Quantifier: makes the preceding group (s) optional http://, https://
: Literal colon character http:
\/\/ Literal forward slashes (escaped) http://
(www\.)? Group: matches 'www.' literally, optional www., (empty)
[-a-zA-Z0-9@:%._\+~#=]{1,256} Character class: matches domain name parts (1-256 chars), including hyphens, alphanumeric, and symbols example, sub-domain
\. Literal dot character (escaped) .com
[a-zA-Z0-9()]{1,6} Character class: matches TLD (1-6 chars), alphanumeric or parentheses com, org, io
\b Word boundary anchor example.com
([-a-zA-Z0-9()@:%_\+.~#?&\/=]*) Group: matches path, query, or fragment (0 or more chars), including symbols /path?query=value#fragment
$ Anchor: asserts position at end of the string http://example.com/path

Matches

  • http://example.com
  • https://www.example.com
  • ftp://sub.domain.co.uk/path?query=value
  • http://localhost:8080
  • https://user:pass@secure.example.org

Does not match

  • example.com
  • http//example.com
  • http:/example.com
  • http://example..com
  • http://-example.com
  • http://example.com/ path
  • http://example.c

Common pitfalls

  • Fails to match URLs without protocol (e.g., 'example.com').
  • Does not validate TLDs strictly (e.g., 'http://example.c' matches).
  • Overly permissive with symbols in domain names (e.g., 'http://exa_mple.com').
  • Does not handle internationalized domain names (IDNs).
  • May match invalid paths with spaces (though \b helps).

Performance notes

Low risk of catastrophic backtracking due to bounded quantifiers. Anchors (^ and $) ensure full-string matching, preventing partial matches.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this regex validate all valid URLs?

No. It matches common URL patterns but does not enforce strict RFC compliance (e.g., valid TLDs, IDNs, or port numbers).

Why is the TLD limited to 1-6 characters?

To balance flexibility with practicality. Some TLDs (e.g., '.com') are short, while others (e.g., '.museum') are longer, but 6 chars covers most.

Can this regex match URLs with authentication (e.g., http://user:pass@example.com)?

Yes, due to the character class in the path/query group, but it does not enforce strict validation of the authentication part.

Why use \b (word boundary) after the TLD?

To ensure the TLD is followed by a non-word character (e.g., '/', '?', or end of string), preventing invalid matches like 'example.come'.