Regex to Match Image File Names

Match image file names by their extension with this regex. Validates common image formats including JPG, PNG, GIF, SVG, and WebP. Free regex tool.

Regular Expression

/^[\w.-]+\.(?:jpg|jpeg|png|gif|bmp|svg|webp|ico|tiff?)$/i

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
[\w.-]Character class — matches any one of: \w.-
+Matches the preceding element one or more times (greedy)
\.Matches a literal dot
(?:Start of non-capturing group
jMatches the literal character 'j'
pMatches the literal character 'p'
gMatches the literal character 'g'
|Alternation — matches the expression before OR after the pipe
jMatches the literal character 'j'
pMatches the literal character 'p'
eMatches the literal character 'e'
gMatches the literal character 'g'
|Alternation — matches the expression before OR after the pipe
pMatches the literal character 'p'
nMatches the literal character 'n'
gMatches the literal character 'g'
|Alternation — matches the expression before OR after the pipe
gMatches the literal character 'g'
iMatches the literal character 'i'
fMatches the literal character 'f'
|Alternation — matches the expression before OR after the pipe
bMatches the literal character 'b'
mMatches the literal character 'm'
pMatches the literal character 'p'
|Alternation — matches the expression before OR after the pipe
sMatches the literal character 's'
vMatches the literal character 'v'
gMatches the literal character 'g'
|Alternation — matches the expression before OR after the pipe
wMatches the literal character 'w'
eMatches the literal character 'e'
bMatches the literal character 'b'
pMatches the literal character 'p'
|Alternation — matches the expression before OR after the pipe
iMatches the literal character 'i'
cMatches the literal character 'c'
oMatches the literal character 'o'
|Alternation — matches the expression before OR after the pipe
tMatches the literal character 't'
iMatches the literal character 'i'
fMatches the literal character 'f'
fMatches the literal character 'f'
?Makes the preceding element optional (zero or one times)
)End of group
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates filenames with common image file extensions. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string.

[\w.-]+ — Matches the filename portion before the extension. The character class includes word characters (\w matches letters, digits, underscore), dots (for names like photo.min), and hyphens. The + requires at least one character.

. — Matches the literal dot separating the filename from the extension.

(?:jpg|jpeg|png|gif|bmp|svg|webp|ico|tiff?) — A non-capturing group with alternatives for common image extensions:

  • jpg and jpeg for JPEG images (the most common photo format)
  • png for Portable Network Graphics (supports transparency)
  • gif for Graphics Interchange Format (supports animation)
  • bmp for Bitmap images
  • svg for Scalable Vector Graphics
  • webp for Google WebP format (modern web optimization)
  • ico for icon files (favicons)
  • tiff? matches both tif and tiff (the ? makes the second f optional)

$ — Anchors the match at the end of the string.

The i flag makes matching case-insensitive, so both .JPG and .jpg are accepted. This pattern is useful for file upload validation, image processing pipelines, and content management systems where you need to verify that uploaded files have image extensions. Note that this checks only the extension, not the actual file content. A file could have an image extension but contain non-image data.

Example Test Strings

InputExpected
photo.jpgMatch
logo.PNGMatch
icon.svgMatch
document.pdfNo Match
animation.gifMatch

Try It — Interactive Tester

//i
gimsuy
No matches found.
Pattern: 54 charsFlags: iMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →