Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
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) | http or https |
| |
Alternation: OR operator between 'https?' and 'ftp' | N/A |
ftp |
Matches literal 'ftp' | ftp |
) |
End of the capturing group | N/A |
:// |
Matches literal '://' | :// |
[^\s/$.?#] |
Negated character class: matches any character except whitespace, '/', '$', '.', '?', or '#' | a, b, 1, A, - |
\. |
Escaped literal '.': matches a dot character | . |
[^\s]* |
Negated character class: matches zero or more characters that are not whitespace | example, com/path, 123 |
$ |
Anchor: asserts position at end of the string | End of 'http://example.com' |
Matches
http://example.comhttps://sub.domain.co.uk/pathftp://files.example.orghttp://a.bhttps://1.2.3.4
Does not match
http://example..comhttp://example.com/path with spacehttp://.example.comhttp://example.com$pathhttp://example.com?queryexample.comhttp://https:/example.com
Common pitfalls
- Fails to validate proper domain structure (e.g., allows 'http://a..b').
- Does not enforce TLD length or validity (e.g., 'http://a.b' is accepted).
- Allows paths starting immediately after domain (e.g., 'http://example.compath').
- Does not handle internationalized domain names (IDNs).
- May match unintended strings if not anchored properly (e.g., 'prefixhttp://example.com').
Performance notes
No catastrophic backtracking risk. The pattern is linear in complexity. Anchors (^ and $) ensure efficient matching. Negated character classes ([^\s]) are efficient.