In Google Apps Scripts, I’m trying to match a URL using RegExp using the following function.
function testRegex(){
var str = "href='https://sites.google.com/a/domain.com/image-store/images/Image1.jpg?attredirects=0'";
var regex = new RegExp('http[:a-zA-Z\.\/\-_]{0,100}Image1.jpg', 'gi');
str = str.replace(regex,"new_url");
Logger.log(str);
}
When I input the same regexp and string into the regular expression tester at http://www.regular-expressions.info/javascriptexample.html , it works. However, it doesn’t work in Google Apps Scripts.
Any ideas why?
EDIT:
I figured the problem is with the underscore. Replacing with \w helps. So, when I replace the Regex with
https[\.a-zA-Z0-9\/+:\w-]{0,100}Image1.jpg
IT WORKS.
But, it still doesn’t match an underscore. For example, it doesn’t work with the following the URL
https://sites.google.com/a/domain.com/image-store/_/rsrc/1351707816362/images/Image1.jpg
I didn’t debug your code, but I tried it on repl.it and verified that it is not correct in Chrome’s V8 JavaScript either. I suspect there is a bug here that is unrelated to Apps Script.
EDIT: This works:
It didn’t match underscores because you didn’t specify an underscore in the character class.