正在删除文件扩展名和特殊字符

Removing file extension and special characters

本文关键字:特殊字符 扩展名 文件 删除      更新时间:2023-09-26

嗨,我试图删除文件扩展名和特殊的chracter,但我的问题有点困难,那是我现有的代码

<script>
var pathname = window.location.pathname;
pathname = pathname.replace(/[^a-zA-Z0-9]/g,' ');
window.location="http://mimuz.com/search.php?keywords="+pathname;
</script>

在路径名称中有三个部分,例如

/videos-of-stars-and-stellites-etc_fe5eb9bf1.html

最好地解释url

恒星和恒星等的视频=视频名称

下划线_

fe5eb9bf1=这是唯一的视频ID

比最后一次

.html=这是扩展

我想做的是我想删除任何斜杠、连字符、点,并用空格替换它们,最后我想从我的url中完全删除_fe5eb9bf1.html这种类型的移植知道吗?

所以最后我会得到这样的结果

videos of stars and stellites etc
pathname.replace(/_[^.]+.[a-z]+$/, '').replace(/[^a-zA-Z0-9]/g,' ');

小提琴在这儿:http://jsfiddle.net/bPVbk/

最简单的方法就是使用split方法。类似的东西

var pathname = window.location.pathname;
var videoname = pathname.split("_")[0];
videoname = videoname.replace(/[^a-zA-Z0-9]/g,' ');