Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches URLs starting with http://, https://, or ftp://, followed by a domain/path and optional query/fragment, ensuring no whitespace or invalid characters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'http://example.com'
( Start of a capturing group N/A
https? Matches 'http' followed by an optional 's' http or https
| Alternation: matches either the preceding or following pattern N/A
ftp Matches the literal string 'ftp' ftp
) End of the capturing group N/A
: Matches the literal colon character :
\/\/ Matches the literal string '://' (escaped forward slashes) ://
[^\s\/$\.?#] Matches any single character except whitespace, /, $, ., ?, or # e in 'example'
. Matches any single character except newline x in 'example.com'
[^\s]* Matches zero or more characters that are not whitespace ample.com/path?query=1
$ 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/page?query=1
  • https://192.168.1.1:8080

Does not match

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

Common pitfalls

  • Forgetting to escape forward slashes in the protocol separator (://).
  • Assuming the pattern matches all valid URLs (e.g., it rejects URLs with spaces or certain special characters).
  • Overlooking that the first character after :// must not be whitespace or /$?.#.
  • Not accounting for internationalized domain names (IDNs) or Unicode characters.

Performance notes

The pattern is generally efficient due to the use of negated character classes and anchors. However, the `[^\s]*` at the end could match very long strings, potentially causing performance issues if the input is extremely large. The alternation `(https?|ftp)` is simple and does not introduce significant backtracking.

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 pattern validate all possible valid URLs?

No. It only matches URLs starting with http://, https://, or ftp://, followed by a domain/path. It does not validate the full URL structure (e.g., TLDs, port numbers, or query parameters).

Why is the first character after :// restricted to `[^\s\/$\.?#]`?

This ensures the URL starts with a valid domain/path character (e.g., 'e' in 'example.com'). It prevents invalid sequences like ':///' or '://.' immediately after the protocol.

Can this pattern match URLs with Unicode characters?

No. JavaScript regex does not natively support Unicode property escapes (e.g., \p{L}) in all environments. For Unicode support, consider using the `u` flag and Unicode-aware patterns.

How would I modify this to include 'ftps' as a protocol?

Add 'ftps?' to the alternation group: `(https?|ftps?|ftp)`. This would match 'ftp', 'ftps', 'http', and 'https'.