path.basename(path[, suffix])
-
path
<string> -
suffix
<string> 要删除的可选后缀¥
suffix
<string> An optional suffix to remove -
返回:<string>
¥Returns: <string>
path.basename()
方法返回 path
的最后一部分,类似于 Unix basename
命令。忽略尾随 目录分隔符。
¥The path.basename()
method returns the last portion of a path
, similar to
the Unix basename
command. Trailing directory separators are
ignored.
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'
尽管 Windows 通常以不区分大小写的方式处理文件名(包括文件扩展名),但此函数不会。例如,C:\\foo.html
和 C:\\foo.HTML
指的是同一个文件,但 basename
将扩展名视为区分大小写的字符串:
¥Although Windows usually treats file names, including file extensions, in a
case-insensitive manner, this function does not. For example, C:\\foo.html
and
C:\\foo.HTML
refer to the same file, but basename
treats the extension as a
case-sensitive string:
path.win32.basename('C:\\foo.html', '.html');
// Returns: 'foo'
path.win32.basename('C:\\foo.HTML', '.html');
// Returns: 'foo.HTML'
如果 path
不是字符串,或者如果给定 suffix
并且不是字符串,则抛出 TypeError
。
¥A TypeError
is thrown if path
is not a string or if suffix
is given
and is not a string.