javascript 如何检查路径是绝对路径还是相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21698906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:29:47  来源:igfitidea点击:

How to check if a path is absolute or relative

javascriptnode.jspathdirectoryabsolute

提问by Manuel Di Iorio

UNIX absolute path starts with '/', whereas Windows starts with alphabet 'C:' or '\'. Does node.js has a standard multiplatform function to check if a path is absolute or relative ?

UNIX 绝对路径以“/”开头,而 Windows 以字母“C:”或“\”开头。node.js 是否具有标准的多平台功能来检查路径是绝对路径还是相对路径?

回答by chresse

Since node version 0.12.0 you can use the path.isAbsolute(path)function from the pathmodule.

从节点版本 0.12.0 开始,您可以使用path模块中的path.isAbsolute(path)功能。

i.e:

IE:

var path = require('path');
if(path.isAbsolute(myPath)) {
    //...
}

回答by Denys Séguret

You could use

你可以用

path.resolve(yourPath)===yourPath

If your path isn't normalized, use

如果您的路径未标准化,请使用

path.resolve( yourPath ) == path.normalize( yourPath )

回答by peoro

As commented to dystroy's answer, the proposed solutions don't work if an absolute path is not already normalized (for example the path: ///a//..//b//./).

正如对dystroy的回答所评论的那样,如果绝对路径尚未标准化(例如路径:),则建议的解决方案不起作用///a//..//b//./

A correct solution is:

正确的解决方法是:

path.resolve(yourPath) === path.normalize(yourPath)


As Marc Diethelmsuggests in the comments, this has still some issues, since path.resolveremoves trailing slashes while path.normalizedoesn't.

正如Marc Diethelm在评论中所建议的那样,这仍然存在一些问题,因为path.resolve删除了尾部斜杠而path.normalize没有删除。

I'm not sure how these function exactly behave (as you can read in the comments), anyway the following snippet seem to work fine at least in Linux environments:

我不确定这些函数的确切行为(正如您在评论中所读到的),无论如何,以下代码段至少在 Linux 环境中似乎可以正常工作:

path.resolve(yourPath) === path.normalize(yourPath).replace( RegExp(path.sep+'$'), '' );

回答by TomDotTom

This is a little convoluted, but the most robust way I've found using just the (pre node 0.12.0) path module

这有点令人费解,但我发现的最强大的方法是仅使用(节点前 0.12.0)路径模块

function isAbsolute(p) {
    return path.normalize(p + '/') === path.normalize(path.resolve(p) + '/');
}

It should be noted that path.isAbsolute exists from node 0.12.0 onwards.

需要注意的是 path.isAbsolute 从节点 0.12.0 开始就存在了。

回答by Javier Ros

I have no idea about node.js, but you can see the source of path.js in github: https://github.com/joyent/node/blob/master/lib/path.js

我对node.js一无所知,但是你可以在github中看到path.js的源码:https: //github.com/joyent/node/blob/master/lib/path.js

You can see:

你可以看到:

// windows version
exports.isAbsolute = function(path) {
    var result = splitDeviceRe.exec(path),
    device = result[1] || '',
    isUnc = device && device.charAt(1) !== ':';
    // UNC paths are always absolute
    return !!result[2] || isUnc;
};

And:

和:

// posix version
exports.isAbsolute = function(path) {
    return path.charAt(0) === '/';
};

回答by Shadrack N. John

    isRelative(url){
        return (/^(\.){1,2}(\/){1,2}$/.test(url.slice(0,3)) ||
        /(\/){1,2}(\.){1,2}(\/){1,2}/.test(url)); 
    }

This makes it easy to check whether a path is relative despite of absence of node path module API.

尽管没有节点路径模块 API,这使得检查路径是否是相对的很容易。

(/^(\.|~){1,2}(\/){1,2}$/.test(url.slice(0,3))

this part checks if the path string starts with the "./" or "../" or "~/". If it does, Boolean true is returned. Otherwise the next test is executed.

这部分检查路径字符串是否以“./”或“../”或“~/”开头。如果是,则返回布尔值 true。否则执行下一个测试。

/(\/){1,2}(\.){1,2}(\/){1,2}/.test(url)

This just checks if the path string has either "/./" or "/../". and returns true on any and false on neither.

这只是检查路径字符串是否包含“/./”或“/../”。并在任何情况下都返回 true ,在任何情况下都返回 false 。

If any of the two tests is true then the path string is relative.

如果两个测试中的任何一个为真,则路径字符串是相对的。

For windows.

对于窗户。

    isRelative(url){
        return (/^(\.){1,2}(\){1,2}$/.test(url.slice(0,3)) ||
        /(\){1,2}(\.){1,2}(\){1,2}/.test(url)); 
    }