Created
August 16, 2019 03:19
-
-
Save eligrey/55eab71c73117df451e3dbe82accdb22 to your computer and use it in GitHub Desktop.
filename from path regexes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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