Skip to content

Instantly share code, notes, and snippets.

@nichoth
Last active November 18, 2022 18:05
Show Gist options
  • Save nichoth/3b135c85c65902677739d9ebdd177bb9 to your computer and use it in GitHub Desktop.
Save nichoth/3b135c85c65902677739d9ebdd177bb9 to your computer and use it in GitHub Desktop.
Get the last segment of a path or url
// get the last segment of a path or URL
function getLastItem (thePath) {
const pathArr = thePath.split('/')
// handle trailing slash
return (pathArr[pathArr.length - 1] || pathArr[pathArr.length - 2])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment