Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if sentence contains string [duplicate]

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.

like image 977
wish_i_was_nerdy Avatar asked Nov 22 '10 18:11

wish_i_was_nerdy


Video Answer


1 Answers

The method you're looking for is indexOf (Documentation). Try the following

if (sentence.indexOf('Hello World') >= 0) {    alert('Yes'); } else {    alert('No'); } 
like image 80
JaredPar Avatar answered Sep 22 '22 18:09

JaredPar