Javascript Google应用程序脚本正则表达式语法
这到底是怎么回事??就我的生命而言,我无法理解Javascript Google应用程序脚本正则表达式语法,javascript,regex,Javascript,Regex,这到底是怎么回事??就我的生命而言,我无法理解 link = item.getChild("link", atom).getAttribute("href").getValue().replaceText("(https:\/\/www\.google\.com\/url\?rct=j&sa=t&url=|&ct=ga.*)",""); 我试过各种排列方式。”“项”是来自atom文件的xml中的子项: item = items[i]; 例如,这种方法可以: link
link = item.getChild("link", atom).getAttribute("href").getValue().replaceText("(https:\/\/www\.google\.com\/url\?rct=j&sa=t&url=|&ct=ga.*)","");
我试过各种排列方式。”“项”是来自atom文件的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.*","");
不,也不是那样。有什么好处?更糟糕的是,一些缓存正在进行,并不能真正帮助测试脚本。顺便说一句,应用程序脚本是JavaScript,它以特定的方式处理正则表达式。同样地,
String.replace()
函数也以两种特殊方式工作-字符串替换和正则表达式匹配替换。您正在尝试以字符串替换方式使用正则表达式
比照
您需要使用.replace(模式,替换)
版本。这里的模式是一个正则表达式对象而不是字符串。在方法中定义模式对象,或将其定义为单独的声明(newregexp()
)
比照
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());