我想比较天气输入路径是否在同一文件夹中
问题:输入路径是否在同一个文件夹中?
假设我当前的路径是f://learning/java
现在我位于名为java
的文件夹中,无论path
直接属于java
我的函数应该返回真
。
假设我有这么多路径:
f://learning/java/first/
truef://learning/java/1.java
truef://learning/java/machine/learning
假f://learning/java/a/b
假f://learning/java/
truef://learning/f
假
我已经尝试过,如下所示:
function pathDirectlyBelongsToSameFoler(currentPath, incomingPath) {
if (currentPath == incomingPath) return true;
// other comparision code i don't know
}
var currentPath = 'f://learning/java';
console.log(pathDirectlyBelongsToSameFoler(currentPath, 'f://learning/java/first'));
console.log(pathDirectlyBelongsToSameFoler(currentPath, 'f://learning/java/1.java'));
console.log(pathDirectlyBelongsToSameFoler(currentPath, 'f://learning/java/machine/learning'));
console.log(pathDirectlyBelongsToSameFoler(currentPath, 'f://learning/java/a/b'));
console.log(pathDirectlyBelongsToSameFoler(currentPath, 'f://learning/java/'));
console.log(pathDirectlyBelongsToSameFoler(currentPath, 'f://learning/f'));
最佳答案
您可以构建动态正则表达式并针对传入路径进行测试
function pathTester(currentPath, incomingPath) {
if(incomingPath == currentPath) return true
currentPath += currentPath.endsWith('/') ? '' : '/'
let reg = new RegExp(String.raw `^${currentPath}[^\/]*\/?$`)
return reg.test(incomingPath)
}
var currentPath = 'f://learning/java';
console.log(pathTester(currentPath, 'f://learning/java/first'));
console.log(pathTester(currentPath, 'f://learning/java/1.java'));
console.log(pathTester(currentPath, 'f://learning/java/machine/learning'));
console.log(pathTester(currentPath, 'f://learning/java/a/b'));
console.log(pathTester(currentPath, 'f://learning/java/'));
console.log(pathTester(currentPath, 'f://learning/f'));
关于javascript - 如何比较路径是否在同一个文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58668327/