这到底是怎么回事?对于我的生活,我无法理解。
link = item.getChild("link", atom).getAttribute("href").getValue().replaceText("(https:\/\/www\.google\.com\/url\?rct=j&sa=t&url=|&ct=ga.*)","");
我尝试了各种各样的排列。 'item'是来自原子文件的xml中的子项:
item = items[i];
例如,这可以起作用:
link = item.getChild("link", atom).getAttribute("href").getValue().replace("https://www.google.com/url?rct=j&sa=t&url=","");
这里的目标,就像它可能已经变得明显一样,是围绕它在警报源中产生的结果摆脱谷歌的垃圾软件。我也尝试过:
link = item.getChild("link", atom).getAttribute("href").getValue().replace("https://www.google.com/url?rct=j&sa=t&url=","").replaceText("&ct=ga.*","");
无济于事。还有:
link = item.getChild("link", atom).getAttribute("href").getValue().replace("https://www.google.com/url?rct=j&sa=t&url=","");
link = link.replaceText("&ct=ga.*","");
不,不是这样。是什么赋予了?更糟糕的是,一些缓存正在进行,并没有真正帮助测试脚本。那,顺便说一下,是here。
答案 0 :(得分:1)
Apps脚本是以特定方式处理正则表达式的JavaScript。同样地,String.replace()
函数也以两种特定方式工作 - 字符串替换和正则表达式匹配替换。您正在尝试以字符串替换方式使用正则表达式。
比照。 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace
您需要使用.replace(pattern, replacement)
版本。这里的模式是一个正则表达式对象而不是一个字符串。在方法中定义模式对象或作为单独的声明(new RegExp()
)
比照。 https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Tldr;要内联定义正则表达式对象,请使用/
将其括起来而不是"
。
所以:
/(https:\/\/www\.google\.com\/url\?rct=j&sa=turl=|&ct=ga.*)/
我认为无论如何都不会对你有用。
尝试匹配,而不是删除您不想要提取的内容而不删除您不想删除的内容。
link = (function (link) {
var match = link.match(/#.*[?&]url=([^&]+)(&|$)/);
return(match ? match[1] : "");
}(item.getChild("link", atom).getAttribute("href").getValue());