2

I am trying to check if a string contains a particular word, not just a substring.

Here are some sample inputs/outputs:

var str = "This is a cool area!";
containsWord(str, "is"); // return true
containsWord(str, "are"); // return false
containsWord(str, "area"); // return true

The following function will not work as it will also return true for the second case:

function containsWord(haystack, needle) {
     return haystack.indexOf(needle) > -1;
}

This also wouldn't work as it returns false for the third case:

function containsWord(haystack, needle) {
     return (' ' +haystack+ ' ').indexOf(' ' +needle+ ' ') > -1;
}

How do I check if a string contains a word then?

2
  • couldn't this be "fixed" by inserting a space before and after the search term? Commented Nov 23, 2016 at 12:27
  • Your problem is the ponctuation (!) ... adding the spaces are good (on both to prevent problem on first or last word) but since you have some non alphabetic character against the last word, this doesn't work. You could simply remove them (but not perfect) or use a simple RegExp that will filter them Commented Nov 23, 2016 at 12:43

3 Answers 3

7

Try using regex for that, where the \b metacharacter is used to find a match at the beginning or end of a word.

var str = "This is a cool area!";

function containsWord(str, word) {
  return str.match(new RegExp("\\b" + word + "\\b")) != null;
}

console.info(containsWord(str, "is")); // return true
console.info(containsWord(str, "are")); // return false
console.info(containsWord(str, "area")); // return true

Sign up to request clarification or add additional context in comments.

4 Comments

You could simply check for not null return of the match since you don't need the value return ;) this would be much simpler.
Yes, that is also true. I modified the snippet.
Hi, is there a way to make containsWord('hello there!', 'there!') also return true?
The function does exactly what you want, it will tell if the desired word is in the string. You can modify the word you want to find, and remove all special characters from it before passing it to the containsWord function like this "there!".replace(/[^\w\s]/gi, '').
4

You can remove all special characters and then split string with space to get list of words. Now just check id searchValue is in this word list

function containsWord(str, searchValue){
  str = str.replace(/[^a-z0-9 ]/gi, '');
  var words = str.split(/ /g);
  return words.indexOf(searchValue) > -1
}

var str = "This is a cool area!";
console.log(containsWord(str, "is")); // return true
console.log(containsWord(str, "are")); // return false
console.log(containsWord(str, "area")); // return true

Comments

2

$(function(){


function containsWord(haystack, needle) {
     
     return haystack.replace(/[^a-zA-Z0-9 ]/gi, '').split(" ").indexOf(needle) > -1;
}
  
  var str = "This is a cool area!";
console.log(containsWord(str, "is")); // return true
console.log(containsWord(str, "are")); // return false
console.log(containsWord(str, "area")); 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

5 Comments

It returns false for the third case
Please explain your attempt. What have you changed and why and how does it solves the problem
@Rayon I was just about to update your answer with my logic as our answers were similar, but when I clicked save, you removed your answer
@Gauravjoshi just out of curiosity, why have you added jQuery in your answer?
@Rajesh not so important. just wanted to add snippet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.