Get the base URL without any parameters
Drop the query string and fragment.
function baseUrl(url) { return url.split(/[?#]/)[0]; }
Input & expected output
Input
"https://example.com/unsubscribe?token=abc"Expected output
"https://example.com/unsubscribe"The edge case
Input
"https://example.com/#top"Expected output
"https://example.com/"Know the limits
- The original removed only the query; a fragment is not a parameter either, so it is dropped too.
- Use new URL() when you need to keep or edit specific parameters.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Get the base URL without any parameters from the original 1loc collection by Nguyen Huu Phuoc, with shawphy. Copyright (c) 2020 phuoc-ng. MIT licensed.
Also drops the fragment, which the original kept when no query was present. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Capitalize a string — Uppercase the first Unicode code point and preserve the remaining text.
- Reverse a string — Reverse Unicode code points without splitting surrogate pairs.
- Check a palindrome — Compare a string with its code-point reversal.