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

时间:2021-06-22 21:47:17

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是否有标准的多平台函数来检查路径是绝对路径还是相对路径?

6 个解决方案

#1


19  

It's a little bit late but for others searching on the same issue:

这有点晚了但是对于其他人在同一个问题上搜索:

since node version 0.12.0 you can use the path.isAbsolute(path) function from the path module.

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

i.e:

即:

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

#2


8  

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//./).

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

A correct solution is:

正确的解决方案是:

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

As Marc Diethelm suggests in the comments, this has still some issues, since path.resolve removes trailing slashes while path.normalize doesn'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+'$'), '' );

#3


8  

You could use

你可以用

path.resolve(yourPath)===yourPath

If your path isn't normalized, use

如果您的路径未规范化,请使用

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

#4


3  

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开始存在。

#5


1  

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) === '/';
};

#6


0  

    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.

这只是检查路径字符串是“/./”还是“/../”。并且在any上都返回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)); 
    }

#1


19  

It's a little bit late but for others searching on the same issue:

这有点晚了但是对于其他人在同一个问题上搜索:

since node version 0.12.0 you can use the path.isAbsolute(path) function from the path module.

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

i.e:

即:

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

#2


8  

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//./).

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

A correct solution is:

正确的解决方案是:

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

As Marc Diethelm suggests in the comments, this has still some issues, since path.resolve removes trailing slashes while path.normalize doesn'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+'$'), '' );

#3


8  

You could use

你可以用

path.resolve(yourPath)===yourPath

If your path isn't normalized, use

如果您的路径未规范化,请使用

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

#4


3  

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开始存在。

#5


1  

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) === '/';
};

#6


0  

    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.

这只是检查路径字符串是“/./”还是“/../”。并且在any上都返回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)); 
    }