Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(https?|ftp)://[^\s/$.?#].[^\s]*$

Matches URLs starting with http://, https://, or ftp:// followed by a domain and path, ensuring no whitespace or invalid characters immediately after the protocol.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'http://example.com'
( Start of a non-capturing group (though it captures here due to alternation) N/A
https? Matches 'http' followed by an optional 's' http or https
| Alternation: matches either the preceding or following pattern Separates 'https?' and 'ftp'
ftp Matches the literal string 'ftp' ftp
) End of the non-capturing group N/A
:// Matches the literal string '://' :// in 'http://'
[^\s/$.?#] Negated character class: matches any character except whitespace, '/', '$', '.', '?', or '#' e in 'example.com'
. Matches any single character (except newline) x in 'example.com'
[^\s]* Negated character class: matches zero or more characters that are not whitespace ample.com/path
$ Anchor: asserts position at end of the string End of 'http://example.com/path'

Matches

  • http://example.com
  • https://example.com/path
  • ftp://files.example.org
  • http://sub.domain.co.uk/path?query=1#frag

Does not match

  • http:// example.com
  • https://example.com path
  • ftp://example..com
  • http://example.com$path
  • http://example.com/path with space

Common pitfalls

  • The pattern allows invalid domains like 'http://.com' because [^\s/$.?#] can match a dot immediately after '://'.
  • The dot after [^\s/$.?#] is greedy and may match unintended characters if not constrained.
  • No validation of domain structure (e.g., 'http://com' is technically matched).
  • The pattern does not enforce valid TLDs or domain labels.

Performance notes

The pattern is generally efficient due to the use of negated character classes and anchors. However, the greedy [^\s]* at the end could lead to catastrophic backtracking if the input contains long sequences of non-whitespace characters. Anchoring (^ and $) ensures the regex matches the entire string, which is good for validation but may fail on 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

Why does the pattern allow 'http://.com'?

Because the character class [^\s/$.?#] matches a dot (.), and the subsequent dot (.) matches any character (including another dot). To prevent this, add a constraint like [^\s/.] after '://'.

How can I ensure the domain has at least one dot?

Modify the pattern to require a dot in the domain part, e.g., ^(https?|ftp)://[^\s/$.?#]+\.[^\s]*$.

Does this pattern validate URLs according to RFC standards?

No. This is a simplified pattern for basic URL matching and does not fully comply with RFC 3986 or other URL standards. For strict validation, use a dedicated URL parsing library.

Why is the dot after [^\s/$.?#] not escaped?

In JavaScript regex, a dot (.) matches any character except newline by default. Escaping it (\.) would match a literal dot, which is not the intended behavior here.