File Extension Validation Regex (Images)
• CronOS Team
regexfile-extensionvalidationtutorialimages
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 (Images)
Validate image file extensions including jpg, jpeg, png, gif, bmp, and svg using regex pattern.
Pattern Breakdown
regex
\.(jpg|jpeg|png|gif|bmp|svg)$
Components
| Component | Description | Matches |
|---|---|---|
\. | Extension dot | Literal dot (escaped) |
(jpg|jpeg|png|gif|bmp|svg) | Extension | One of the allowed extensions |
$ | End anchor | Ensures match to string end |
Character Classes
\.- Literal dot (escaped, since.matches any character)(jpg|jpeg|png|gif|bmp|svg)- Alternation group: matches one of the listed extensions$- End anchor ensures extension is at the end
Examples
Valid:
image.jpgphoto.jpegpicture.pnganimation.gifbitmap.bmpvector.svgmy.image.pngfile-name.jpeg
Invalid:
image.JPG(uppercase - use case-insensitive flag)image.jpg.txt(not at end)image(no extension)image.pdf(not an image extension).jpg(no filename)image.jpg.backup(extension not at end)
Implementation
JavaScript
javascript
const imageExtRegex = /\.(jpg|jpeg|png|gif|bmp|svg)$/i; // 'i' flag for case-insensitive
imageExtRegex.test('image.jpg'); // true
imageExtRegex.test('photo.PNG'); // true (with 'i' flag)
imageExtRegex.test('picture.png'); // true
imageExtRegex.test('document.pdf'); // false (not an image)
imageExtRegex.test('image'); // false (no extension)
Python
python
import re
image_ext_regex = r'\.(jpg|jpeg|png|gif|bmp|svg)$'
bool(re.match(image_ext_regex, 'image.jpg', re.IGNORECASE)) # True
bool(re.match(image_ext_regex, 'photo.PNG', re.IGNORECASE)) # True
bool(re.match(image_ext_regex, 'document.pdf', re.IGNORECASE)) # False
Go
go
imageExtRegex := regexp.MustCompile(`(?i)\.(jpg|jpeg|png|gif|bmp|svg)$`) // (?i) for case-insensitive
imageExtRegex.MatchString("image.jpg") // true
imageExtRegex.MatchString("photo.PNG") // true
imageExtRegex.MatchString("document.pdf") // 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 image formats
- No filename validation: Doesn't validate the filename part
- Extension spoofing: Can be bypassed by renaming files
When to Use
- Image file upload validation
- File type filtering
- Quick extension checking
- Client-side validation
- When you need to filter image files
For production, consider:
- Using case-insensitive matching
- Validating actual file content (MIME type)
- Checking file signatures/magic numbers
- Supporting additional image formats if needed
- 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!