Regex to Match File Extensions
Extract file extensions from filenames or file paths with this regex pattern. Captures extensions like .js, .txt, or .html accurately. Free tool.
Regular Expression
/\.([a-zA-Z0-9]{1,10})$/
Token Breakdown
| Token | Description |
|---|---|
| \. | Matches a literal dot |
| ( | Start of capturing group |
| [a-zA-Z0-9] | Character class — matches any one of: a-zA-Z0-9 |
| {1,10} | Matches between 1 and 10 times |
| ) | End of group |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex matches and captures file extensions at the end of filenames. Here is the token-by-token breakdown:
. — Matches a literal dot (period) character. The backslash escapes the dot to prevent it from being interpreted as the regex wildcard metacharacter. This dot separates the filename from its extension.
([a-zA-Z0-9]{1,10}) — A capturing group matching the extension itself. The character class allows uppercase letters (A-Z), lowercase letters (a-z), and digits (0-9). The quantifier {1,10} requires between 1 and 10 characters, which covers all common file extensions from single-character ones like .c to longer ones like .typescript. Setting an upper limit of 10 prevents matching excessively long strings that are unlikely to be real extensions.
$ — Anchors the match at the end of the string. This ensures the extension is at the end of the filename, not a dot in the middle of the path.
The captured group (group 1) contains the extension without the dot, making it easy to extract and use in your code. This pattern matches extensions like .js, .html, .py, .dockerfile, and .config.
Note that this pattern matches the last extension only. For files with multiple extensions like archive.tar.gz, it would match only .gz. If you need to capture compound extensions, you would need a modified pattern. The pattern also does not validate whether the extension corresponds to a known file type; it simply captures the alphanumeric string after the last dot.
Example Test Strings
| Input | Expected |
|---|---|
| script.js | Match |
| archive.tar.gz | Match |
| README | No Match |
| styles.min.css | Match |
| document.pdf | Match |
Try It — Interactive Tester
Match Highlighting(1 match)
Matches & Capture Groups
22 charsFlags: noneMatches: 1Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match Unix File Paths
/^(?:\/[\w.-]+)+\/?$/
Regex to Match Windows File Paths
/^[a-zA-Z]:\\(?:[\w.-]+\\)*[\w.-]*$/
Regex to Match Image File Names
/^[\w.-]+\.(?:jpg|jpeg|png|gif|bmp|svg|webp|ico|tiff?)$/i
Regex to Match Semantic Versioning
/^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?(?:\+[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?$/