我使用一个功能,我从另一张贴在这里有:
How to get the file name from a full path using JavaScript?
要返回网页的当前文件名
var url=window.location.pathname;
var filename = url.replace(/^.*[\\\/]/, '');
alert(filename);
我很好奇,是否有可能从最后剥离.html而仍然使用这种语法。
我使用一个功能,我从另一张贴在这里有:
How to get the file name from a full path using JavaScript?
要返回网页的当前文件名
var url=window.location.pathname;
var filename = url.replace(/^.*[\\\/]/, '');
alert(filename);
我很好奇,是否有可能从最后剥离.html而仍然使用这种语法。
我认为最简单的方法将是
var filename = url.replace(/^.*[\\\/]/, '').replace(".html", "");
请指定使用要管理,要更严格的分机,:
.replace(/^.*[\/](.*)[.](html|jsp|php)/, '$1');
要么是更通用,使用:
.replace(/^.*[\/](.*)[.][a-zA-Z0-9]{2,5}/, '$1');
第二个将允许从2个字符的扩展(例如.do
)至5个字符(例如.xhtml
),其也可以包含数字(例如.php3
)。
@ user1821973我不知道这个正则表达式是IE8的问题的原因您会议:它必须来自脚本中的其他地方。但无论如何,我很高兴我可以帮助';)' – sp00m
这是可能的,对不起,我直接跳到这一结论,在这里让我编辑:*正则表达式可能在IE8的作品,因为IE8支持正则表达式;然而,在我的脚本中未预见到的东西阻止了这个声明的工作。 – Deprecated
您可以使用捕捉条款代替:
var url=window.location.pathname;
alert(url)
var filename = url.replace(/^.*[\\\/](.*).html$/, '$1');
alert(filename);
这是所有答案中最全面的答案,所以它是我接受的答案;然而所有的答案都很棒! – Deprecated