File Extension Regex
Extracts and validates the file extension of a filename, requiring at least one character before the final dot.
Regex Pattern
^.+\.([a-zA-Z0-9]+)$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| .+ | Matches one or more of any character (the filename stem), greedily |
| \. | Matches a literal dot separating the stem from the extension |
| ( | Opens a capturing group for the extension itself |
| [a-zA-Z0-9]+ | Matches one or more letters or digits, the extension characters |
| ) | Closes the capturing group so the extension can be extracted |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern matches a filename that ends in a dot followed by an alphanumeric extension, capturing the extension in group 1. Because .+ is greedy, it matches up to the last dot in the filename, so multi-dot names like archive.tar.gz correctly extract gz rather than tar.gz.
Why it works
The leading .+ requires at least one character before the dot, which is what excludes dotfiles like .gitignore that have no filename stem before their leading dot. Since .+ is greedy, the regex engine backtracks from the end of the string until it finds the rightmost dot that still allows the trailing [a-zA-Z0-9]+ to match, guaranteeing the final extension is the one captured.
Common use cases
- Extracting a file extension to route uploads to the right handler
- Validating that a filename has an extension before allowing an upload
- Filtering directory listings for files of a particular type
- Building a MIME-type lookup keyed off the captured extension
Edge cases
- Multi-part extensions like archive.tar.gz only capture the final segment, gz, not tar.gz
- Dotfiles like .gitignore or .env are rejected because .+ needs at least one character before the dot
- A trailing dot with nothing after it, like notes., is rejected since [a-zA-Z0-9]+ requires at least one character
- Extensions with unusual characters, like a version tag file.v1.2, still match because digits are allowed in the extension class
Limitations
- Does not validate the extension against a known whitelist of real file types
- Extensions containing characters outside [a-zA-Z0-9], such as a stray hyphen, will not match
- Does not account for case sensitivity conventions of specific filesystems
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const fileExtRegex = /^.+\.([a-zA-Z0-9]+)$/;
const match = 'archive.tar.gz'.match(fileExtRegex);
console.log(match?.[1]); // 'gz'Common Mistakes
Using a lazy .+? before the dot, which grabs the first dot instead of the last in multi-dot filenames
Fix: Keep .+ greedy so it backtracks to the final dot, correctly capturing gz from archive.tar.gz
Forgetting the leading .+ requirement, causing dotfiles like .gitignore to be treated as having an extension
Fix: Require at least one character before the dot with .+\. so dotfiles are excluded
Restricting the extension class to letters only, rejecting valid numeric extensions like .mp3 or .7z
Fix: Use [a-zA-Z0-9]+ to allow both letters and digits in the extension
Performance Notes
- The greedy .+ can cause some backtracking on long filenames, but since the input is typically short this is negligible
- Anchoring with ^ and $ prevents partial matches inside a longer string
- For extension extraction only (no full-string validation), a non-anchored /\.([a-zA-Z0-9]+)$/ is slightly cheaper
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |
Work with this pattern
Take this expression further — test it against your own data, adapt it in the builder, or read up on the syntax behind it.
Regex Tester
Test a regular expression against sample text and see every match highlighted live.
Regex Generator
Build a pattern by clicking blocks — no syntax memorisation required.
Regular Expressions Guide
What regex is, how the syntax works, and the mistakes that trip people up.
Regex Cheat Sheet
Every token, quantifier, and assertion on one printable page.
Python Regex Guide
The re module end to end — search, match, findall, groups, and substitution.
Regex Pattern Library
100+ ready-to-use patterns for email, URLs, dates, IPs, and more.