2013-07-01 100 views
0

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

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:""}]); 

火柴和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。如果有帮助,我可以添加更多的代码。

+0

你的意思是splitAndJoinArray.push([matches2 [A] .replace( “”)...... – mplungjan

+0

感谢该mplungjan - 会 – user905752

+0

感谢mplungjan。这几乎奏效了,在对你的解决方案进行了一些小修改之后,我将它修改为:splitAndJoinArray.push([matches2 [一个],{orig:matches2 [a],修改:matches2 [a] .replace(“。htm”,“”)}]); – user905752

回答

0

mplungjan指出我在正确的方向。下面条从页面源的.htm: “HTM”

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

好的,所以代码[matches2 [a] .replace(“。htm” ,“”)就是你需要的 – mplungjan

+0

是的,再次感谢 – user905752