Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/url/basename.ts>basename

The Deno Standard Library
Latest
function basename
import { basename } from "https://deno.land/std@0.224.0/url/basename.ts";

Returns the base name of a URL or URL string, optionally removing a suffix.

Trailing /s are ignored. If no path is present, the host name is returned.

Examples

Basic usage

import { basename } from "https://deno.land/std@0.224.0/url/basename.ts";

basename("https://deno.land/std/assert/mod.ts"); // "mod.ts"

basename(new URL("https://deno.land/std/assert/mod.ts")); // "mod.ts"

basename("https://deno.land/std/assert/mod.ts?a=b"); // "mod.ts"

basename("https://deno.land/std/assert/mod.ts#header"); // "mod.ts"

basename("https://deno.land/"); // "deno.land"

Removing a suffix

import { basename } from "https://deno.land/std@0.224.0/url/basename.ts";

basename("https://deno.land/std/assert/mod.ts", ".ts"); // "mod"

basename(new URL("https://deno.land/std/assert/mod.ts"), ".ts"); // "mod"

basename("https://deno.land/std/assert/mod.ts?a=b", ".ts"); // "mod"

basename("https://deno.land/std/assert/mod.ts#header", ".ts"); // "mod.ts"

Defining a suffix will remove it from the base name.

Parameters

url: string | URL

The URL from which to extract the base name.

optional
suffix: string

An optional suffix to remove from the base name.

Returns

string

The base name of the URL.