Although this question has already been answered by comments, I will make the correct answer.
An important issue for understanding this problem is exec behavior when the regular expression has the g flag. Which, when called, will sequentially try to search for the "next" match, even if you pass another line. Here is the documentation link on MDN.
And although MDN states that you should take care not to recreate the RegExp object (even the literal), because it could reset the lastIndex . At least in Script applications this is not so. If the regex literal is used in the same place over and over again, Script applications cache the regex and reuse the same object.
These two combined effects meant that you unexpectedly triggered this โnext matchโ behavior on your code.
The simplest solution for you is to simply omit the g flag, since you do not need it (you get only the first result). But you could also fix this by replacing the string var myRegexp = /: (.+)/g; on var myRegexp = new RegExp(': (.+)','g'); by forcing Apps Script to give you a new object.
I think the good lesson we can learn from this is this: don't use a flag if you don't need one. Sometimes we are lazy and set flags, without hesitation, "just in case".
source share