File & Path
File Extension
Captures the file extension from a filename.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("\\.([A-Za-z0-9]+)$", "");
const input = "document.pdf";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"\.([A-Za-z0-9]+)$")
input_text = "document.pdf"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\.([A-Za-z0-9]+)$`)
input := `document.pdf`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
regexengine-agnostic
\.([A-Za-z0-9]+)$Raw source: \.([A-Za-z0-9]+)$
How it works
`\.` matches the literal dot. `([A-Za-z0-9]+)$` captures alphanumeric characters up to the end of the string.
Examples
Input
document.pdfMatches
.pdf
Input
archive.tar.gzMatches
.gz
Input
no-extensionNo match
—Common use cases
- •File filters
- •MIME type detection
- •Upload validation
Related patterns
Image File Extension
File & PathMatch common image file extensions: jpg, jpeg, png, gif, webp, svg, avif, bmp, ico, tif, tiff.
Video File Extension
File & PathMatch common video file extensions: mp4, mov, avi, mkv, webm, m4v, flv, wmv, mpg, mpeg.
Windows File Path
File & PathMatches absolute Windows file paths (e.g., C:\Users\file.txt).
Unix File Path
File & PathMatches absolute Unix/Linux file paths.