我想比较天气输入路径是否在同一个文件夹中。
问题:输入路径是否在同一个文件夹中?
假设我当前的路径是f://learning/java
现在,我在名为java的文件夹中,无论哪个path直接属于java,我的函数都应该返回true。
假设我有以下几条路径:
f://learning/java/first/ truef://learning/java/1.java truef://learning/java/machine/learning falsef://learning/java/a/b falsef://learning/java/ truef://learning/f false我让尝试了,就像下面这样:
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'));
发布于 2019-11-02 05:03:46
您可以构建动态正则表达式并对传入路径进行测试。
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'));
https://stackoverflow.com/questions/58668327
复制相似问题