Image File Regex

\.(jpe?g|png|gif|webp|svg|bmp|ico)$

Match common image file extensions.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern \.(jpe?g|png|gif|webp|svg|bmp|ico)$:

  • $ - End of string
  • ? - Optional (zero or one)
  • (...) - Capturing group
  • | - Alternation (OR)

Examples

Input Result Explanation
photo.jpg ✓ Match JPEG image
icon.svg ✓ Match SVG vector
doc.pdf ✗ No Match Not an image

Code Examples

JavaScript

JavaScript
const pattern = /\.(jpe?g|png|gif|webp|svg|bmp|ico)$/;
const text = "photo.jpg";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'\.(jpe?g|png|gif|webp|svg|bmp|ico)$'
text = "photo.jpg"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/\.(jpe?g|png|gif|webp|svg|bmp|ico)$/';
$text = "photo.jpg";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "\\.(jpe?g|png|gif|webp|svg|bmp|ico)$";
String text = "photo.jpg";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Image upload validation, media processing

Frequently Asked Questions

What is the regex pattern for image file regex?
The regex pattern is: \.(jpe?g|png|gif|webp|svg|bmp|ico)$. Match common image file extensions.
How do I use the image file regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('\\.(jpe?g|png|gif|webp|svg|bmp|ico)$'); or const regex = /\.(jpe?g|png|gif|webp|svg|bmp|ico)$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'\.(jpe?g|png|gif|webp|svg|bmp|ico)$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for image file regex?
Image upload validation, media processing

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: