1

What the hell is wrong with this?? For the life in me I can't figure it out.

link = item.getChild("link", atom).getAttribute("href").getValue().replaceText("(https:\/\/www\.google\.com\/url\?rct=j&sa=t&url=|&ct=ga.*)","");

I've tried all sorts of permutations. 'item' is a child in an xml from an atom file:

item = items[i];

This, for instance, works:

link = item.getChild("link", atom).getAttribute("href").getValue().replace("https://www.google.com/url?rct=j&sa=t&url=","");

The goal here, as it might've become apparent, is to get rid of Google's crapware around the results it yields in an alerts feed. I also tried:

link = item.getChild("link", atom).getAttribute("href").getValue().replace("https://www.google.com/url?rct=j&sa=t&url=","").replaceText("&ct=ga.*","");

No avail. And also:

link = item.getChild("link", atom).getAttribute("href").getValue().replace("https://www.google.com/url?rct=j&sa=t&url=","");
link = link.replaceText("&ct=ga.*","");

Nope, not that either. What gives? To make matters worse, some caching is going on and doesn't really help testing the script. That, btw, is here.

1 Answer 1

1

Apps Script is JavaScript which deals with Regular Expressions in a specific way. Likewise the String.replace() function also works in two particular ways — string replacement and regex match replacement. You are trying to use regex in the string replacement manner.

Cf. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

You need to use the .replace(pattern, replacement) version. pattern here is a regex object and not a string. Define the pattern object right there in the method or as a separate declaration (new RegExp())

Cf. https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

Tldr; To define a regex object inline use / to enclose it and not ".

So:

/(https:\/\/www\.google\.com\/url\?rct=j&sa=turl=|&ct=ga.*)/ which I don't think will work for you in any case.

Try matching and not removing what you don't what you want to extract and not removing what you don't.

link = (function (link) {
  var match = link.match(/#.*[?&]url=([^&]+)(&|$)/);
  return(match ? match[1] : "");
}(item.getChild("link", atom).getAttribute("href").getValue());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.