我有一些客户端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。如果有帮助,我可以添加更多的代码。
你的意思是splitAndJoinArray.push([matches2 [A] .replace( “”)...... – mplungjan
感谢该mplungjan - 会 – user905752
感谢mplungjan。这几乎奏效了,在对你的解决方案进行了一些小修改之后,我将它修改为:splitAndJoinArray.push([matches2 [一个],{orig:matches2 [a],修改:matches2 [a] .replace(“。htm”,“”)}]); – user905752