Skip to content

Instantly share code, notes, and snippets.

@eligrey
Created August 16, 2019 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eligrey/55eab71c73117df451e3dbe82accdb22 to your computer and use it in GitHub Desktop.
Save eligrey/55eab71c73117df451e3dbe82accdb22 to your computer and use it in GitHub Desktop.
filename from path regexes
const pathFileNameMatcher = /\/?(?<fileName>[^/]+(?<fileExtension>\.[^/.]*)?)\/*$/;
const pathFilePrefixMatcher = /\/?(?<filePrefix>[^/]+)(?<fileExtension>\.[^/.]*)?\/*$/;
'test/foo/|foo|.test.enc/'.match(pathFilePrefixMatcher).groups.filePrefix == '|foo|.test'
const matches = new URL('https://your-url-here/example.txt').pathname.match(pathFileNameMatcher);
const fileName =
(matches && matches.groups && matches.groups.fileName) || 'file';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment