Node.js/file

来自康健生活
Kangkk​(留言 | 贡献)2021年6月26日 (六) 08:02的版本 (创建页面,内容为“== 引言 == == 文件名相关 == 获取文件名后缀 <syntaxhighlight lang="javascript"> /** * 获取文件后缀 * @param {string} item 文件名路径,eq:…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

引言

文件名相关

获取文件名后缀

/**
 * 获取文件后缀
 * @param {string} item 文件名路径,eq: /test/path/kangkk.txt
 * @returns {string} 文件名后缀
 * 使用方法:getExt('/test/path/kangkk.txt') ===> 'txt'
 */
const getExt = (item: string) => {
  const match = /.\.(\w+)$/.exec(item);
  return match && match[1];
};

获取文件名,不带后缀

/**
 * 获取文件名,不带后缀
 * @param {string} item 文件名路径,eq: /test/path/kangkk.txt
 * @returns {string} 文件名后缀
 * 使用方法:getNameNoExt('/test/path/kangkk.txt') ===> '/test/path/kangkk'
 */
const getNameNoExt = (item: string) => {
  return item.substring(0, item.lastIndexOf("."));
};