Javascript 如何使用谷歌脚本在字符串中查找文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/30324532/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find text in a string using google script?
提问by Arun Prakash
I tried indexOf(), findText() and other few methods for finding a string pattern in a text in google app script. None of the above method works.
我尝试了 indexOf()、findText() 和其他几种方法来在 google 应用程序脚本的文本中查找字符串模式。以上方法均无效。
var str="task is completed";
I'm getting this string from google spreadsheet.
我从谷歌电子表格中得到这个字符串。
I just want to find whether the above string contains a string "task" .
我只想找到上面的字符串是否包含字符串 "task" 。
回答by Tushar
You need to check if the stris present:
您需要检查是否str存在:
if (str) {
    if (str.indexOf('task') > -1) {
        // Present
    }
}
Alternatively, you can use testand regex:
或者,您可以使用test和regex:
/task/.test("task is completed");
/task/.test(str);
- /task/: Regex to match the 'task'
- test: Test the string against regex and return boolean
- /task/:正则表达式匹配“任务”
- test: 针对正则表达式测试字符串并返回布尔值
回答by Zig Mandel
a simple str.indexOf("test")>=0does it. it works. not sure why you say it doesnt work as you havent shown any code to point out the problem.
if you want to check regardless of case use str.toLowerCase().indexOf("test")
一个简单的str.indexOf("test")>=0就可以了。有用。不知道为什么你说它不起作用,因为你没有显示任何代码来指出问题。
如果你想检查不管大小写str.toLowerCase().indexOf("test")

