如果一个句子包含“Hello World”(不带引号),那么我需要返回 true 并执行某些操作。可能的句子可能是这样的:
var sentence = "This is my Hello World and I like widgets."
var sentence = "Hello World - the beginning of all"
var sentence = "Welcome to Hello World"
if ( sentence.contains('Hello World') ){
alert('Yes');
} else {
alert('No');
}
我知道 .contains 不起作用,所以我正在寻找有用的东西。正则表达式是这里的敌人。
最佳答案
您要查找的方法是 indexOf
( Documentation )。尝试以下方法
if (sentence.indexOf('Hello World') >= 0) {
alert('Yes');
} else {
alert('No');
}
关于javascript - 如果句子包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4248630/