url.stripQuery
v0.1.0 latestRemove the RFC 3986 query string and fragment from a URL, returning only the path portion.
Remove the RFC 3986 query string and fragment from a URL, returning only the path portion.
Signature
function stripQuery(url: string): string
Problem
Extracting the base URL without query parameters or fragments requires manual string splitting that must handle edge cases (no query, no fragment, both present, fragment before query).
How It Works
Strips both ?query and #fragment portions. Returns the URL up to (but not including) the first ? after fragment removal.
Boundaries
- Throws TypeError for non-string input.
- Removes both query and fragment.
- Returns the original string unchanged if no query or fragment is present.
Replaces
Common boilerplate this function replaces:
url.split('?')[0].split('#')[0]
Examples
url.stripQuery("https://example.com/path?q=1#top"); // "https://example.com/path"
url.stripQuery("https://example.com/path"); // "https://example.com/path"
url.stripQuery("https://example.com/path#section"); // "https://example.com/path"
Standards
Caveats
- Does not validate or normalize the URL structure.
FAQ
How to remove query parameters from a URL in JavaScript?
url.stripQuery('https://example.com/path?q=1') returns 'https://example.com/path'.
Does url.stripQuery also remove fragments?
Yes. Both ?query and #fragment are stripped.
What happens if the URL has no query string?
The URL is returned unchanged.
How to get just the base URL without parameters?
url.stripQuery(fullUrl) returns the path portion without query or fragment.
Does url.stripQuery handle URLs with both query and fragment?
Yes. url.stripQuery('https://example.com/path?q=1#top') returns 'https://example.com/path'.