Regex 从url获取特定部分

Regex 从url获取特定部分,regex,Regex,我的window.location是“F:/html5/home.html”,从我的位置我需要得到像这样的文件名“home.html”,要做到这一点,我如何使用正则表达式命令 有人能帮我吗?如果你能灵活地不使用正则表达式,我会这样做: var pathArr = new Array(); pathArr = window.location.split("/"); var file = pathArr[pathArr.length - 1]; 如果您可以灵活地不使用正则表达式,我会这样做: va

我的window.location是“F:/html5/home.html”,从我的位置我需要得到像这样的文件名“home.html”,要做到这一点,我如何使用正则表达式命令


有人能帮我吗?

如果你能灵活地不使用正则表达式,我会这样做:

var pathArr = new Array();
pathArr = window.location.split("/");
var file = pathArr[pathArr.length - 1];

如果您可以灵活地不使用正则表达式,我会这样做:

var pathArr = new Array();
pathArr = window.location.split("/");
var file = pathArr[pathArr.length - 1];
这就行了

[^/]+$
它匹配字符串末尾的非正斜杠字符的子字符串

var s = "F:/html5/home.html";
//forwards slashes must be escaped in JavaScript regex as it is the delimiter
alert(s.match(/[^\/]+$/)[0]);
这就行了

[^/]+$
它匹配字符串末尾的非正斜杠字符的子字符串

var s = "F:/html5/home.html";
//forwards slashes must be escaped in JavaScript regex as it is the delimiter
alert(s.match(/[^\/]+$/)[0]);

你不需要做第一个数组,你不需要做第一个数组。