JavaScript / ECMAScript

Image File Extension in JS

Match common image file extensions: jpg, jpeg, png, gif, webp, svg, avif, bmp, ico, tif, tiff.

Try it in the JS tester →

Pattern

regexJS
\.(jpe?g|png|gif|webp|svg|avif|bmp|ico|tiff?)$   (flags: i)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\.(jpe?g|png|gif|webp|svg|avif|bmp|ico|tiff?)$", "i");
const input = "photo.jpeg";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

\. matches the literal dot. The alternation group covers all common web and raster image extensions. jpe?g covers both jpg and jpeg. tiff? covers tif and tiff. The i flag makes matching case-insensitive so .PNG and .JPG also match.

Examples

Input

photo.jpeg

Matches

  • .jpeg

Input

logo.SVG

Matches

  • .SVG

Input

document.pdf

No match

Same pattern, other engines

← Back to Image File Extension overview (all engines)