Skip to content

onlyPath

Extract only the path from an URI with optional query and fragments.

For example, all these parameters will return /path:

  • /path
  • /path?query=thing
  • /path#fragment
  • /path?query=thing#fragment

Available since v1.0.0

import { onlyPath } from '@helpers4/url';
// or, from the all-in-one package (same code, one install):
import { onlyPath } from 'helpers4/url';
onlyPath(url: string): string
ParameterTypeDescription
urlstringThe URL string to be processed. Can be `string`, `undefined`, or `null`.

string — The URL string without query and fragment, or undefined if the input is undefined, or null if the input is null.

Strips query parameters and fragments from a URL path.

onlyPath('/path?query=thing#fragment')
// => '/path'

View source on GitHub