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


simple str.indexOf("test")>=0

does it. it works. not sure why you say it doesn't work as you haven't provided any code to point out the problem.
if you want to check regardless of case usestr.toLowerCase().indexOf("test")



+3


source







All Articles