标签云

微信群

扫码加入我们

WeChat QR Code

This question already has an answer here:

If a sentence contains "Hello World" (no quotes) then I need to return true and do something. Possible sentences could be like this:

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');
}

I know the .contains does not work, so I'm looking for something does work. Regex is the enemy here.


Don't just try it. Do it. ;)

2018年07月17日09分48秒

Stephen, is there an extra semantic that !== false adds here or is it just for readability?

2018年07月18日09分48秒

Eh, sorry. I just deleted that part of the comment, Jared. In fact, if the string doesn't exist, indexOf returns -1 not false.

2018年07月17日09分48秒

Stephen, ok that was my understanding and I was wondering how !== false would work in that situation (still fairly new to javascript).

2018年07月18日09分48秒

Stephen, no worries. It actually prompted me to do further reading on strict vs. non-strict equality in javascript and I learned quite a bit. So overall a win :)

2018年07月18日09分48秒