12

Does Javascript have a built-in function to see if a word is present in a string? I'm not looking for something like indexOf(), but rather:

find_word('test', 'this is a test.') -> true
find_word('test', 'this is a test') -> true
find_word('test', 'I am testing this out') -> false
find_word('test', 'test this out please') -> true
find_word('test', 'attest to that if you would') -> false

Essentially, I'd like to know if my word appears, but not as part of another word. It wouldn't be too hard to implement manually, but I figured I'd ask to see if there's already a built-in function like this, since it seems like it'd be something that comes up a lot.

Mala
  • 14,178
  • 25
  • 88
  • 119
  • 1
    possible duplicate of [How can I check if one string contains another substring in JavaScript?](http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring-in-javascript) – Jeremy J Starcher Aug 25 '14 at 20:39
  • 2
    @JeremyJStarcher did you read the question? – Mala Aug 25 '14 at 20:40
  • Actually, yes, I see the one spot where it's false, because of it being 'testing', and thus, that's the difference. But your question reads like *"I don't like the syntax of indexOf(), what can I use instead"* instead of actually explaining that indexOf() doesn't do what I want because ... explanation – CaffGeek Aug 25 '14 at 20:42
  • @Mala Yes I did. The linked answer discusses the regex, though briefly. – Jeremy J Starcher Aug 25 '14 at 20:43
  • 1
    @JeremyJStarcher The person asking the question is asking a fundamentally different question, and scanning down the answers I don't see any that would answer the question above. My apologies if I'm missing something. – Mala Aug 25 '14 at 20:45

3 Answers3

24

You can use split and some:

function findWord(word, str) {
  return str.split(' ').some(function(w){return w === word})
}

Or use a regex with word boundaries:

function findWord(word, str) {
  return RegExp('\\b'+ word +'\\b').test(str)
}
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    The former wouldn't handle punctuation properly, but the latter looks like it'd be what I need. Thanks! – Mala Aug 25 '14 at 20:43
  • I suppose to make the first one work, one could use `.split(/\s*\b\s*/)` as per http://stackoverflow.com/questions/24718348/how-to-parse-string-into-words-and-punctuation-marks-using-javascript. Didn't know about the .some() function, so thanks for teaching me about that too :) – Mala Aug 25 '14 at 20:50
  • The two functions aren't equivalent. `findWord('test', 'test-team')` will return *false* in the first case and *true* in the second. The first should split on `\b` or the second use `' '`. The second is likely very much faster for big strings. – RobG Aug 28 '14 at 01:24
3

The JavaScript includes() method determines whether a string contains the characters of a specified string. This method returns true if the string contains the characters, and false if not.

Syntax:

string.includes(searchvalue, start)

Parameter values:

Parameter      Description
searchvalue    Required. The string to search for
start          Optional. Default 0. At which position to start the search

Example:

const sentence = 'The quick brown fox jumps over the lazy dog.';
const word = 'fox';
console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);
karel
  • 5,489
  • 46
  • 45
  • 50
-1

Moderns browsers have the Array.prototype.includes(), which determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Here's an example:

const ShoppingList = ["Milk", "Butter", "Sugar"];

console.log(`Milk ${ShoppingList.includes("Milk") ? "is" : "is not"} in the shopping list.`);

console.log(`Eggs ${ShoppingList.includes("Eggs") ? "is" : "is not"} in the shopping list.`)
Jakye
  • 6,440
  • 3
  • 19
  • 38