As for Jacob, the function “contains” has a missing opening bracket.
return str.indexOf(text) >= 0);
it should be
return (str.indexOf(text) >= 0);
I selected Patricks answer and adapted it to function
Example
function InString(myString,word)
{
var regex = new RegExp( '\\b' + word + '\\b' );
var result = regex.test( myString );
return( result );
}
Call a function with
var ManyWords='This is a list of words';
var OneWord='word';
if (InString(ManyWords,OneWord)){alert(OneWord+' Already exists!');return;}
This will return False, because although words exist in the ManyWords variable, this function only shows the exact match for the word.
source
share