获取不带文件后缀的页面名

Get page name without the file suffix

本文关键字:后缀 文件 获取      更新时间:2023-09-26

如何从页面名称中摆脱文件后缀(.html/.php等)?
我目前使用这个来获取页面名称

location.pathname.substring(location.pathname.lastIndexOf("/") + 1);

你可以使用regex:

location.pathname.match(/('w+)'.?'w*$/).pop()

A bit explanation:

('w+) -这是文件名。

'.? -点可以。

'w*?—后缀可以是。

$ -它必须在url

的末尾

检查这个简单的子字符串函数,从0点到"."字符串的位置提取文本。

var str = "yourfilename.html";
alert(str.substring(0, str.indexOf(".")));