Back to Home

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!

Generate Regex Pattern

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

ComponentDescriptionMatches
\.Extension dotLiteral dot (escaped)
(jpg|jpeg|png|gif|bmp|svg)ExtensionOne of the allowed extensions
$End anchorEnsures 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.jpg
  • photo.jpeg
  • picture.png
  • animation.gif
  • bitmap.bmp
  • vector.svg
  • my.image.png
  • file-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

  1. Case-sensitive by default: Requires case-insensitive flag for uppercase extensions
  2. No MIME type validation: Only checks extension, not actual file content
  3. Limited extensions: Only covers common image formats
  4. No filename validation: Doesn't validate the filename part
  5. 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!

Generate Regex Pattern