Regex for File Extension
Matches filenames and extracts the file extension. Captures extensions up to 10 characters long.
Pattern
/^.+\.([a-zA-Z0-9]{1,10})$/Pattern Breakdown
| Token | Explanation |
|---|---|
| ^ | Start of string |
| .+ | One or more characters (filename) |
| \. | Literal dot before extension |
| ([a-zA-Z0-9]{1,10}) | Extension: 1-10 alphanumeric characters (captured) |
| $ | End of string |
Test Examples
| Input | Match? |
|---|---|
| document.pdf | Match |
| archive.tar.gz | Match |
| image.png | Match |
| noextension | No match |
| .hidden | No match |
Common Use Cases
- File upload validation
- File type filtering
- Directory listing
Variations
\.(jpe?g|png|gif|svg|webp)$Specific image extensions
\.(js|ts|jsx|tsx)$JavaScript/TypeScript files
Other Regex Patterns
Want to test this pattern with your own data?
Open the Regex Tester