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).

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.pdf

Matches

  • .pdf

Input

archive.tar.gz

Matches

  • .gz

Input

no-extension

No match

Common use cases

  • File filters
  • MIME type detection
  • Upload validation

Related patterns