当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js path.parse(path)用法及代码示例

path.parse(path)

添加于:v0.11.15

参数

path.parse() 方法返回一个对象,其属性表示 path 的重要元素。尾随目录分隔符被忽略,请参阅 path.sep

返回的对象将具有以下属性:

例如,在 POSIX 上:

path.parse('/home/user/dir/file.txt');
// Returns:
// { root: '/',
//   dir: '/home/user/dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' }
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
"  /    home/user/dir / file  .txt "
└──────┴──────────────┴──────┴─────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)

在 Windows 上:

path.parse('C:\\path\\dir\\file.txt');
// Returns:
// { root: 'C:\\',
//   dir: 'C:\\path\\dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' }
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
" C:\      path\dir   \ file  .txt "
└──────┴──────────────┴──────┴─────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)

如果 path 不是字符串,则会抛出 TypeError

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 path.parse(path)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。