How can I find text in a string using google script?
I tried indexOf (), findText () and several other methods to find a string pattern in text in google app script. None of the above methods work.
var str="task is completed";
I am getting this line from google spreadsheet.
I just want to find if the specified string contains the string "task".
+3
source to share
2 answers
You need to check for availability str
:
if (str) {
if (str.indexOf('task') > -1) {
// Present
}
}
Alternatively, you can use test
and regex
:
/task/.test("task is completed");
/task/.test(str);
-
/task/
: regular expression to match "tasks" -
test
: check the regex string and return boolean
+8
source to share