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 match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

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