File Extension in JS
Captures the file extension from a filename.
Try it in the JS tester →Pattern
regexJS
\.([A-Za-z0-9]+)$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).
How the pattern 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
—