[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Get the file name from a URL

Get the file name from a URL

JavaScript

const fileName = (url: string): string => url.substring(url.lastIndexOf('/') + 1)

TypeScript

const fileName = (url: string): string => url.substring(url.lastIndexOf('/') + 1)

Examples

fileName('http://domain.com/path/to/document.pdf') // 'document.pdf'