Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created January 22, 2025 18:30
Show Gist options
  • Save DavidWells/ca67d5c06b7b958da4190ffbd3e5c3ad to your computer and use it in GitHub Desktop.
Save DavidWells/ca67d5c06b7b958da4190ffbd3e5c3ad to your computer and use it in GitHub Desktop.
function testFilename(filename) {
const match = filename.match(/^(?:(\d+)[.-])?([^.]+)(?:\.([^.]+))?(?:\.([^.]+))?$/)
if (match) {
const order = match[1]
const base = match[2] ?? filename
const modifier = match[4] ? match[3] : undefined
const extension = match[4] ?? match[3]
console.log({
filename,
order,
base,
modifier,
extension
})
}
}
// Examples with parsed values:
testFilename('01-hello.md') // {order: '01', base: 'hello', modifier: undefined, extension: 'md'}
testFilename('hello.md')
testFilename('hello.test.js') // {order: undefined, base: 'hello', modifier: 'test', extension: 'js'}
testFilename('01.page.test.jsx')
testFilename('hello')
testFilename('wow/hello.md')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment