JavaScript

How to Output File Extension in JavaScript

A memo on how to output file extension in JavaScript. For example, extract file extensions (pdf, txt, html) from URLs obtained with document.location.href. getFileExtension(document.location.href);

Shou Arisaka
1 min read
Oct 31, 2025

A memo on how to output file extension in JavaScript. For example, extract file extensions (pdf, txt, html) from URLs obtained with document.location.href.

function getFileExtension(filename) {
  return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename)[0] : undefined;
}

getFileExtension(document.location.href);

Share this article

Shou Arisaka Oct 31, 2025

๐Ÿ”— Copy Links