Javascript 从path regex获取最后2或3个元素
因此,我目前有一个路径,我试图获取最后3 测试:Javascript 从path regex获取最后2或3个元素,javascript,regex,Javascript,Regex,因此,我目前有一个路径,我试图获取最后3 测试: /testing/path/here/src/handlebar/sample/colors.txt /testing/path/here/src/handlebar/testing/another/colors.txt \/([^/]+\/[^/]+\/[^/]+)\.[^.]+$ sample/colors testing/another/colors 正则表达式: /testing/path/here/src/handlebar/sa
/testing/path/here/src/handlebar/sample/colors.txt
/testing/path/here/src/handlebar/testing/another/colors.txt
\/([^/]+\/[^/]+\/[^/]+)\.[^.]+$
sample/colors
testing/another/colors
正则表达式:
/testing/path/here/src/handlebar/sample/colors.txt
/testing/path/here/src/handlebar/testing/another/colors.txt
\/([^/]+\/[^/]+\/[^/]+)\.[^.]+$
sample/colors
testing/another/colors
结果:
handlebar/sample/colors
testing/another/colors
我希望它做什么:
/testing/path/here/src/handlebar/sample/colors.txt
/testing/path/here/src/handlebar/testing/another/colors.txt
\/([^/]+\/[^/]+\/[^/]+)\.[^.]+$
sample/colors
testing/another/colors
如果有2个目录,然后是项目,它应该使用3,如果它包含单词handlebar,它应该只有两个。我想
(?!.*handlebar)/([^/]+/[^/]+/[^/]+)\.[^.]+$|/([^/]+/[^/]+)\.[^.]+$
可能行
如果周围环境得到支持
(?!.*handlebar)(?<=/)[^/]+/[^/]+/[^/]+(?=\.[^.]+$)|$|(?<=/)([^/]+/[^/]+)(?=\.[^.]+$)
没有正则表达式的javascript解决方案如下所示:
const getTokenizedPath = path => {
const pathArray = path.split('/');
// last element of array looks like "colors.txt" - split by dot and read the first value, removing the extension
pathArray[pathArray.length-1] = pathArray[pathArray.length-1].split('.')[0];
// Remove all elements before the 'handlebar' token and join the remaining values together by '/'.
return pathArray.slice(pathArr2.indexOf('handlebar')+1).join('/');
}
我希望你能像这样 这是明天的动态,您可以根据所需的参数进行传递并获得结果
var res=“/testing/path/here/src/handlebar/sample/colors.txt”;
var res1=“/testing/path/here/src/handlebar/testing/other/colors.txt”;;
结果=(val,text)=>{
var r=val.split(text+'/')[1];
返回r.substr(0,r.lastIndexOf('.');
}
控制台日志(结果(res,“把手”);
控制台日志(结果(res1,“把手”);
您只需为把手后面的所有东西创建一个组,如下所示:
const getTokenizedPath = path => {
const pathArray = path.split('/');
// last element of array looks like "colors.txt" - split by dot and read the first value, removing the extension
pathArray[pathArray.length-1] = pathArray[pathArray.length-1].split('.')[0];
// Remove all elements before the 'handlebar' token and join the remaining values together by '/'.
return pathArray.slice(pathArr2.indexOf('handlebar')+1).join('/');
}
使用命名的捕获组(子路径组包含所需值):
说明:此正则表达式匹配所有以“handlebar/(…任何非空格字符0到无限次)。(任何空格字符1到无限次)结尾的字符。使用全局标志和多行标志,如果要检查一个字符串中用换行符分隔的多个路径,例如
当您使用标签javascript
标记问题时,下面是一些示例代码,如何检索regex组的值
function getSubPath(fullPath = '') {
const regex = /handlebar\/(?<subPath>\S*)\.\S+$/gm
const match = regex.exec(fullPath)
if (match) {
return match.groups.subPath
}
return fullPath // regex.exec did not deliver match
}
getSubPath('/testing/path/here/src/handlebar/sample/colors.txt')
// returns 'sample/colors'
getSubPath('/testing/path/here/src/handlebar/testing/another/colors.txt')
// returns 'testing/another/colors'
函数getSubPath(完整路径=“”){
常量正则表达式=/handlebar\/(?\S*)\.\S+$/gm
const match=regex.exec(完整路径)
如果(匹配){
返回match.groups.subPath
}
返回完整路径//regex.exec未传递匹配项
}
getSubPath('/testing/path/here/src/handlebar/sample/colors.txt')
//返回“样本/颜色”
getSubPath(“/testing/path/here/src/handlebar/testing/other/colors.txt”)
//返回“测试/其他/颜色”
如果没有命名组,只需读取/返回match.groups[1]作为第一个捕获组;索引0表示完全匹配(其中包括“/handlebar”和文件扩展名)您需要这样的“sample/colors.txt”输出还是“sample/colors”输出..请确认一次..谢谢。没有扩展名,您无法使用简单的捕获方法进行提取?