快速regexp获取路径 [英] Quick regexp to get path

查看:69
本文介绍了快速regexp获取路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用regExp提取文件的完整路径

I need to extract the full path to a file using regExp

mydomain.com/path/to/file/myfile.html - > mydomain.com/path/to/file /

/mypath/file.txt - > / mypath /

任何人?

推荐答案

试试这个:

"mydomain.com/path/to/file/myfile.html".replace(/[^\/]*$/, "")
"/path/file.txt".replace(/[^\/]*$/, "")

但你也可以不经常做拆分表达式:

But you can also do it without regular expressions by splitting:

"mydomain.com/path/to/file/myfile.html".split("/").slice(0, -1).join("/")+"/"
"/path/file.txt".split("/").slice(0, -1).join("/")+"/"

这篇关于快速regexp获取路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆