Regex for image file name

Use /^.+\.(?:jpe?g|png|gif|webp|avif|svg)$/i to check that a whole string is a valid for image file name. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^.+\.(?:jpe?g|png|gif|webp|avif|svg)$/i
Open in Regex Tester
InputResult
photo.JPG✓ Match
logo.svg✓ Match
my.image.webp✓ Match
document.pdf✗ No match
png✗ No match
image.png.bak✗ No match
.png✗ No match

How the pattern works

PartMeaning
^.+One or more characters of file name
\.The last dot
(?:jpe?g|png|gif|webp|avif|svg)One of the allowed extensions (jpeg or jpg)
i flagCase-insensitive, so .JPG matches too

Code examples

JavaScript

const re = /^.+\.(?:jpe?g|png|gif|webp|avif|svg)$/i;
re.test(value); // true or false

Python

import re

bool(re.fullmatch(r'.+\.(?:jpe?g|png|gif|webp|avif|svg)', value, re.I))

PHP

preg_match('/^.+\.(?:jpe?g|png|gif|webp|avif|svg)$/i', $value) === 1;

Notes and limitations

Checking the extension is fine for user hints, but never trust it for security — check the file’s contents (magic bytes) on the server.

Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool

Frequently asked questions

What is the regex for image file name?
/^.+\.(?:jpe?g|png|gif|webp|avif|svg)$/i — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: photo.JPG, logo.svg, my.image.webp. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.