Skip to content

Instantly share code, notes, and snippets.

@takumifukasawa
Created November 4, 2020 13:56
Show Gist options
  • Save takumifukasawa/3b20310b8944c88d43bacaab34e1176a to your computer and use it in GitHub Desktop.
Save takumifukasawa/3b20310b8944c88d43bacaab34e1176a to your computer and use it in GitHub Desktop.
typescript: get file extention
/**
* ファイルの拡張子を抜き出す
* ex) test.jpg -> jpg, test.png -> png
*
* @export
* @param {string} str
* @returns {(string | null)}
*/
export default function getExt(str: string): string | null {
// if match regex, it will return [".ext", "ext"].
const result = str.match(/\.([0-9a-zA-Z]+$)/i);
// extが大文字の場合があるので小文字に共通化して吸収する
// 大文字のまま扱いたい場合はないはず
return result ? result[1].toLowerCase() : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment