File Extension Validation Regex (Documents)
• CronOS Team
regexfile-extensionvalidationtutorialdocuments
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
File Extension Validation Regex (Documents)
Validate document file extensions including pdf, doc, docx, txt, rtf, and odt using regex pattern.
Pattern Breakdown
regex
\.(pdf|doc|docx|txt|rtf|odt)$
Components
| Component | Description | Matches |
|---|---|---|
\. | Extension dot | Literal dot (escaped) |
(pdf|doc|docx|txt|rtf|odt) | Extension | One of the allowed extensions |
$ | End anchor | Ensures match to string end |
Character Classes
\.- Literal dot (escaped, since.matches any character)(pdf|doc|docx|txt|rtf|odt)- Alternation group: matches one of the listed extensions$- End anchor ensures extension is at the end
Examples
Valid:
document.pdfreport.docletter.docxnotes.txtdocument.rtffile.odtmy.document.pdffile-name.docx
Invalid:
document.PDF(uppercase - use case-insensitive flag)document.pdf.bak(not at end)document(no extension)document.jpg(not a document extension).pdf(no filename)document.pdf.txt(extension not at end)
Implementation
JavaScript
javascript
const docExtRegex = /\.(pdf|doc|docx|txt|rtf|odt)$/i; // 'i' flag for case-insensitive
docExtRegex.test('document.pdf'); // true
docExtRegex.test('report.DOCX'); // true (with 'i' flag)
docExtRegex.test('letter.docx'); // true
docExtRegex.test('image.jpg'); // false (not a document)
docExtRegex.test('document'); // false (no extension)
Python
python
import re
doc_ext_regex = r'\.(pdf|doc|docx|txt|rtf|odt)$'
bool(re.match(doc_ext_regex, 'document.pdf', re.IGNORECASE)) # True
bool(re.match(doc_ext_regex, 'report.DOCX', re.IGNORECASE)) # True
bool(re.match(doc_ext_regex, 'image.jpg', re.IGNORECASE)) # False
Go
go
docExtRegex := regexp.MustCompile(`(?i)\.(pdf|doc|docx|txt|rtf|odt)$`) // (?i) for case-insensitive
docExtRegex.MatchString("document.pdf") // true
docExtRegex.MatchString("report.DOCX") // true
docExtRegex.MatchString("image.jpg") // false
Limitations
- Case-sensitive by default: Requires case-insensitive flag for uppercase extensions
- No MIME type validation: Only checks extension, not actual file content
- Limited extensions: Only covers common document formats
- No filename validation: Doesn't validate the filename part
- Extension spoofing: Can be bypassed by renaming files
- Missing formats: Doesn't include xls, xlsx, ppt, pptx, etc.
When to Use
- Document file upload validation
- File type filtering
- Quick extension checking
- Client-side validation
- When you need to filter document files
For production, consider:
- Using case-insensitive matching
- Validating actual file content (MIME type)
- Checking file signatures/magic numbers
- Supporting additional document formats (xls, xlsx, ppt, pptx, etc.)
- Server-side validation (never trust client-side only)
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!