Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script findtext searchpattern format?

I am trying to pass in simple regex strings like

findText("/a/");

or

findText(/a/);

but it does not find anything. If I pass in only the text that works like this

findText("a");

How to pass regex strings in there?

like image 706
giorgio79 Avatar asked Feb 28 '26 23:02

giorgio79


1 Answers

It's not super clear in the documentation of findText, but the documentation for replaceText is more clear:

The search pattern is passed as a string, not a JavaScript regular expression object.

The example shown in the documentation of replaceText shows that your 3rd example is the correct one (where the search for a is shown as just the string, "a".

body.replaceText("^.*Apps ?Script.*$", "Apps Script");

Obviously, String.search() will work here as well, but if you're looking to manipulate the attributes of the text, rather than just the string contents, using the built-in javascript function might leave you hanging.

like image 148
pdro Avatar answered Mar 02 '26 12:03

pdro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!