0

我有一些客户端 Javascript,它为使用数组和 .push 方法的 .doc、pdf 和 csv 文件添加文件大小和图标。我想修改代码以从页面 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 将项目添加到数组的末尾。我尝试使用 .replace 从所有页面 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);

match2 工作正常并返回页面上的 htm 链接列表。因为这是我继承的东西,而且我的 JavaScript 不是很出色,所以我不知道如何更改

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

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

4

1 回答 1

0

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

splitAndJoinArray.push([matches2[a],{orig:matches2[a],modified:matches2[a].replace(".htm","")}]);
于 2013-07-02T09:32:43.067 回答