首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较和获取两个路径字符串的差异

如何比较和获取两个路径字符串的差异
EN

Stack Overflow用户
提问于 2021-10-06 06:56:39
回答 2查看 21关注 0票数 0

我有两个文件路径作为字符串:

代码语言:javascript
复制
const filePath = "reports/cucumber/7/cucumber/sample/json/login.json"
const reportFolder = "reports/cucumber/7/cucumber/sample"

我需要比较这两种方法,然后得到如下输出

代码语言:javascript
复制
/json/login.json

我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2021-10-06 07:06:06

您还可以使用String.replace()来获取差异:

代码语言:javascript
复制
const filePath = "reports/cucumber/7/cucumber/sample/json/login.json"
const reportFolder = "reports/cucumber/7/cucumber/sample"

const diff = filePath.replace(reportFolder, '');
console.log('Diff:', diff)

票数 1
EN

Stack Overflow用户

发布于 2021-10-06 07:00:03

代码语言:javascript
复制
const filePath = "reports/cucumber/7/cucumber/sample/json/login.json"
const reportFolder = "reports/cucumber/7/cucumber/sample"

const diff = (diffMe, diffBy) => diffMe.split(diffBy).join('')

const C = diff(filePath, reportFolder)

console.log(C)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69461137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档