Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for image file name
Regex pattern · validation

Regex for image file name

Regex for image file extension: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^[\w ,-]+(?:\.[\w-]+)*\.(?:jpe?g|png|gif|webp|avif|svg)$/i as an image file name regex. It matches "photo.jpg" and "My Image.PNG", and rejects "photo.jpg.exe" and "document.pdf". Extensions can be faked.

Last updated · Maintained by FreeAIToolz · How we test

/^[\w ,-]+(?:\.[\w-]+)*\.(?:jpe?g|png|gif|webp|avif|svg)$/i
  • photo.jpgmatches
  • My Image.PNGmatches
  • banner-2026.webpmatches
  • logo.min.svgmatches
  • photo.jpg.exeno match
  • document.pdfno match
  • .pngno match
  • photono match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
^[\w ,-]+file name characters
(?:\.[\w-]+)*optional extra dotted parts
\.(?:jpe?g|png|gif|webp|avif|svg)$an image extension at the very end

Flags: i makes it case-insensitive.

Examples

InputResult
photo.jpg✓ Matches
My Image.PNG✓ Matches
banner-2026.webp✓ Matches
logo.min.svg✓ Matches
photo.jpg.exe✗ No match
document.pdf✗ No match
.png✗ No match
photo✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Extensions can be faked. Check the file’s MIME type and magic bytes on the server, and treat SVG as untrusted (it can contain scripts).

When to use this pattern

Quick client-side checks on uploads.

Use it in your language

JavaScript

const re = /^[\w ,-]+(?:\.[\w-]+)*\.(?:jpe?g|png|gif|webp|avif|svg)$/i;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^[\w ,-]+(?:\.[\w-]+)*\.(?:jpe?g|png|gif|webp|avif|svg)$', re.IGNORECASE)
bool(pattern.search(value))

PHP

$ok = preg_match('~^[\\w ,-]+(?:\\.[\\w-]+)*\\.(?:jpe?g|png|gif|webp|avif|svg)$~i', $value) === 1;

Java

Pattern pattern = Pattern.compile("^[\\w ,-]+(?:\\.[\\w-]+)*\\.(?:jpe?g|png|gif|webp|avif|svg)$", Pattern.CASE_INSENSITIVE);
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`(?i)^[\w ,-]+(?:\.[\w-]+)*\.(?:jpe?g|png|gif|webp|avif|svg)$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for image file name?
/^[\w ,-]+(?:\.[\w-]+)*\.(?:jpe?g|png|gif|webp|avif|svg)$/i. Extensions can be faked. Check the file’s MIME type and magic bytes on the server, and treat SVG as untrusted (it can contain scripts).
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abcphoto.jpgxyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.