T
ToolPrime

Regex for File Extension

Extracts or validates file extensions.

Pattern

/\.[a-zA-Z0-9]{1,10}$/i

Live Tester

Enter a string to test

Examples

The i flag makes the pattern case-insensitive.

document.pdf
image.png
archive.tar.gz
noextension
file.
.

Pattern Breakdown

\. — literal dot

[a-zA-Z0-9]{1,10}$ — 1-10 alphanumeric chars at end

Code Snippets

Javascript

"document.pdf".match(/\.[a-zA-Z0-9]{1,10}$/i)?.[0]; // ".pdf"

Python

re.search(r"\.[a-zA-Z0-9]{1,10}$", "document.pdf").group()  # ".pdf"

Php

preg_match('/\.[a-zA-Z0-9]{1,10}$/i', "document.pdf", $m); // $m[0] = ".pdf"

Related Patterns

Frequently Asked Questions

What is the regex for File Extension?
The regex pattern for File Extension is /\.[a-zA-Z0-9]{1,10}$/i. Extracts or validates file extensions.
How do I use the File Extension regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the File Extension pattern match?
It matches strings like document.pdf; it rejects strings like noextension. See the Examples section above for the full list of matching and non-matching cases.

Related Tools