Get the base URL without any parameters

Drop the query string and fragment.

JavaScript Version 1.0.0 · MIT · .md

get-the-base-url-without-any-parameters.js
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.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.