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
| Token | Description |
|---|---|
| ^ | 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 |
| j | Matches the literal character 'j' |
| p | Matches the literal character 'p' |
| g | Matches the literal character 'g' |
| | | Alternation — matches the expression before OR after the pipe |
| j | Matches the literal character 'j' |
| p | Matches the literal character 'p' |
| e | Matches the literal character 'e' |
| g | Matches the literal character 'g' |
| | | Alternation — matches the expression before OR after the pipe |
| p | Matches the literal character 'p' |
| n | Matches the literal character 'n' |
| g | Matches the literal character 'g' |
| | | Alternation — matches the expression before OR after the pipe |
| g | Matches the literal character 'g' |
| i | Matches the literal character 'i' |
| f | Matches the literal character 'f' |
| | | Alternation — matches the expression before OR after the pipe |
| b | Matches the literal character 'b' |
| m | Matches the literal character 'm' |
| p | Matches the literal character 'p' |
| | | Alternation — matches the expression before OR after the pipe |
| s | Matches the literal character 's' |
| v | Matches the literal character 'v' |
| g | Matches the literal character 'g' |
| | | Alternation — matches the expression before OR after the pipe |
| w | Matches the literal character 'w' |
| e | Matches the literal character 'e' |
| b | Matches the literal character 'b' |
| p | Matches the literal character 'p' |
| | | Alternation — matches the expression before OR after the pipe |
| i | Matches the literal character 'i' |
| c | Matches the literal character 'c' |
| o | Matches the literal character 'o' |
| | | Alternation — matches the expression before OR after the pipe |
| t | Matches the literal character 't' |
| i | Matches the literal character 'i' |
| f | Matches the literal character 'f' |
| f | Matches 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
| Input | Expected |
|---|---|
| photo.jpg | Match |
| logo.PNG | Match |
| icon.svg | Match |
| document.pdf | No Match |
| animation.gif | Match |
Try It — Interactive Tester
54 charsFlags: iMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match File Extensions
/\.([a-zA-Z0-9]{1,10})$/
Regex to Match Unix File Paths
/^(?:\/[\w.-]+)+\/?$/
Regex to Match Windows File Paths
/^[a-zA-Z]:\\(?:[\w.-]+\\)*[\w.-]*$/
Regex to Match Semantic Versioning
/^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?(?:\+[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?$/