Function getUrlSlug

  • Extracts the slug from a given URL.

    A slug is typically the last part of a URL path, which is used to identify a particular page or resource. This function splits the URL by '/' and returns the last non-empty segment.

    Parameters

    • url: string

      The URL string from which to extract the slug.

    Returns string

    The extracted slug from the URL.

    const slug = getUrlSlug('https://example.com/path/to/resource/');
    console.log(slug); // Output: 'resource'

    const slug2 = getUrlSlug('https://example.com/path/to/resource');
    console.log(slug2); // Output: 'resource'