url.isAbsolute
v0.1.0 latestCheck whether a URL string is absolute (has a scheme:// prefix) per RFC 3986 §3.1.
Check whether a URL string is absolute (has a scheme:// prefix) per RFC 3986 §3.1.
Signature
function isAbsolute(url: string): boolean
Problem
Determining whether a URL is absolute or relative requires checking for a scheme prefix. The RFC 3986 scheme grammar (ALPHA *(ALPHA / DIGIT / + / - / .) ://) is not trivial to match correctly, and ad-hoc checks often miss edge cases like custom schemes or protocol-relative URLs.
How It Works
Uses a regex matching RFC 3986 scheme syntax: ALPHA *( ALPHA / DIGIT / + / - / . ) :// to detect absolute URLs.
Boundaries
- Throws TypeError for non-string input.
- Protocol-relative URLs (//example.com) return false (no scheme).
- Scheme names are case-insensitive in practice, and the regex matches both cases.
Replaces
Common boilerplate this function replaces:
/^https?:\/\//.test(url)
Examples
url.isAbsolute("https://example.com"); // true
url.isAbsolute("ftp://files.example"); // true
url.isAbsolute("//example.com"); // false
url.isAbsolute("/path/to/resource"); // false
Standards
Caveats
- Only checks for scheme:// prefix — does not validate the rest of the URL structure.
- Custom schemes (e.g. myapp://) are recognized as absolute.
FAQ
How to check if a URL is absolute in JavaScript?
url.isAbsolute('https://example.com') returns true. It checks for any valid scheme:// prefix per RFC 3986.
Are protocol-relative URLs considered absolute?
No. url.isAbsolute('//example.com') returns false because there is no scheme.
Does url.isAbsolute support custom schemes?
Yes. Any valid RFC 3986 scheme (e.g. ftp://, myapp://) is recognized.
What is the difference between url.isAbsolute and checking for http?
url.isAbsolute matches any scheme per RFC 3986, not just http/https.
Does url.isAbsolute validate the full URL?
No. It only checks for the presence of a scheme:// prefix.