Image File Extension in GO
Match common image file extensions: jpg, jpeg, png, gif, webp, svg, avif, bmp, ico, tif, tiff.
Try it in the GO tester →Pattern
regexGO
\.(jpe?g|png|gif|webp|svg|avif|bmp|ico|tiff?)$ (flags: i)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)\.(jpe?g|png|gif|webp|svg|avif|bmp|ico|tiff?)$`)
input := `photo.jpeg`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
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.jpegMatches
.jpeg
Input
logo.SVGMatches
.SVG
Input
document.pdfNo match
—