扩展RegExp以获取文件扩展名 [英] Extend RegExp to get the file extension
本文介绍了扩展RegExp以获取文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道,已经有很多基于RegExp的解决方案,但是我找不到适合我需求的解决方案.
I know, there're already a lot of RegExp based solutions, however I couldn't find one that fits to my needs.
我具有以下功能来获取URL的各个部分,但我还需要文件扩展名.
I've the following function to get the parts of an URL, but I also need the file extension.
var getPathParts = function(url) {
var m = url.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
return {
path: m[1],
file: m[2]
};
};
var url = 'path/to/myfile.ext';
getPathParts(url); // Object {path: "path/to", file: "myfile"}
我对regex不太熟悉,也许您可以扩展此给定的regexp,以获取文件扩展名吗?
I'm not very familiar with regex, maybe you can extend this given regexp, to get the file-extension too?
最好的方法是,如果文件扩展名包含第3(第4)个值.例如:
Best way would, if the 3rd (4th) value the file extension contains. E.g.:
return {
path: m[1],
file: m[2],
ext: m[3]
};
推荐答案
只需添加一个捕获组即可获取最后一个\w+
:
Just add a capturing group to get the last \w+
:
var getPathParts = function(url) {
var m = url.match(/(.*)[\/\\]([^\/\\]+)\.(\w+)$/);
return {
path: m[1],
file: m[2],
ext: m[3]
};
};
这篇关于扩展RegExp以获取文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文