我使用一個功能,我從另一張貼在這裏有:
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