Regex for Extracting a File Extension

Matches the file extension at the end of a filename.

Published October 12, 2026

This regex captures the file extension after the last dot in a filename.

How the pattern works

Breaking the pattern down: \. = literal dot; ([A-Za-z0-9]+) = captured extension; $ = at the end.

Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.

Pattern tests

/\.([A-Za-z0-9]+)$/
✓ 'photo.jpg'
✓ 'archive.tar.gz'
✗ 'README'
✗ 'file.'

Practical examples

The pattern

const re = /\.([A-Za-z0-9]+)$/

Test it

const re = /\.([A-Za-z0-9]+)$/
re.test('photo.jpg')   // true
re.test('archive.tar.gz')   // true
re.test('README')   // false
re.test('file.')   // false

In Python

import re
re.search(r'\.([A-Za-z0-9]+)$', text)

Common mistakes

  • Files without extensions.
  • Dotfiles like .gitignore.
  • Multi-part extensions (.tar.gz).

Key takeaways

  • Anchors (^ and $) make the whole string match instead of a part of it.
  • Regex checks format only; verify data separately when it matters.
  • Test with valid and invalid samples.

FAQ

Without regex?

path.extname(file) in Node or file.split('.').pop().

Where can I test this pattern?

Use the Regex Tester on this site, or regex101.com.

More General articles