从网页中删除.htm的javascript示例

javascript example to remove .htm from a web page

本文关键字:javascript 示例 htm 删除 网页      更新时间:2023-09-26

我有一些客户端Javascript,它为.doc、pdf和csv文件添加了文件大小和图标,这些文件使用数组和.push方法。我想修改代码,从页面URL中删除.htm。以下是我在一个名为getAnchorTags:的函数中的代码

splitAndJoinArray.push([matches[a],{orig:matches[a],modified:"<img class ='MIME_content' src='" + getMIMEImage(href)+ "' alt='" + getMIMEAlt(href) + "'>" + matches[a] + " (" + getFileType(href) + getFileSize(href) + "kb)"}]);

使用.push将项添加到数组末尾,效果很好。我尝试过使用.replaces从所有页面URL中删除.htm,但失败了:

splitAndJoinArray.replace([/.htm/g,{orig:matches2[a],modified:""}]); 

Matches和matches2使用一些正则表达式来获得所需的文件扩展名:

var matches = str.match( /<a['s]+[^>]*?href['s]?=['s'"'']*([^"]*?'.(pdf|ppt|csv|xls|doc))['"'']*.*?>([^<]+|.*?)?/gi);
var matches2 = str.match( /<a['s]+[^>]*?href['s]?=['s'"'']*([^"]*?'.(htm))['"'']*.*?>([^<]+|.*?)?/gi);

matches2工作正常,并返回页面上的htm链接列表。因为这是我继承的东西,而我的JavaScript并没有达到什么水平,我不知道如何更改

splitAndJoinArray.replace([/.htm/g,{orig:matches2[a],modified:""}]); 

我知道去掉.htm是不正确的。如果有帮助的话,我可以添加更多的代码。

mplungjan为我指明了正确的方向。以下从页面源中删除.htm:

splitAndJoinArray.push([matches2[a],{orig:matches2[a],modified:matches2[a].replace(".htm","")}]);