2

I am trying to search for exact string/word within a string/sentence with includes() and I want it to return true if it does. But I get returned "true" even if word matches part of a bigger word. How do I approach searching for exact match? I realize that includes() is case sensetive but I want it to be length sensitive so to say, I understand I should probably somehow define what a word in a string for js is but not sure how to do it with includes(). Thanks

var sentence = 'The quick brown fox jumps over the lazy dog.';
var word = 'own';
console.log(`The word "${word}" ${sentence.includes(word)? 'is' : 'is not'} in the sentence`);
// wanted output: "The word "own" is not in the sentence"
// real output: "The word "own" is in the sentence"
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
Sanchez Panza
  • 381
  • 1
  • 11
  • 1
    You can't do it with String#includes, because that's not what String#includes does. Use something else like `(new RegExp( \`\\b${RegExp.escape(word)}\\b\` )).test( sentence )`, where RegExp.escape is the function defined in this answer: https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript – Paul Aug 04 '19 at 17:27
  • Actually you should just split on non-word characters and check if the array includes your word `sentence.split( /\W+/g ).includes( word )` – Paul Aug 04 '19 at 20:16

4 Answers4

2

includes tries to search any sequence which matches the passed string ( parameter ), in this case brown has own in it so it returns true, where as you want it to match only when exact word own is found you can use search and regex,

var sentence = 'The quick brown fox jumps over the lazy dog.';

let wordFounder = word => {
  let reg = new RegExp(`\\b${word}\\b`)
  console.log(`The word "${word}" is ${sentence.search(reg) !== -1 ? '' : 'not'} 
in the sentence`);  
}

wordFounder("own")
wordFounder("brown")
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • weird but this code return false for words that should return true and I cannot fix it at a glance, basically it just always returns false for me. gonna go read up on regExp – Sanchez Panza Aug 04 '19 at 17:42
  • @SanchezPanza what do you mean by that ? can you please elaborate ? – Code Maniac Aug 04 '19 at 17:45
  • Using your code if the word is "brown" for example it should say that it's in the sentance, but it always says it's not. (for example, if I use sentence.search(reg) , it should return true if the word is in the sentence and false if its not right?) – Sanchez Panza Aug 04 '19 at 17:48
  • @SanchezPanza i missed escapping , look at the updated one, i have made an generic function with some example words – Code Maniac Aug 04 '19 at 17:52
  • Thanks mr.Maniac! so the trouble was with the \\b and whats the difference between this and \b ? Also I didnt know that you dont have to put vars in brackets when defining a function in es6 style – Sanchez Panza Aug 04 '19 at 17:58
  • 1
    Just note that this will also match things like: `" "`, `"."`, `""` and `"quick brown"`. – MinusFour Aug 04 '19 at 18:01
  • 1
    It will also match things like `\\s`, `.*`, `|`, and `monkey|` because there is no escaping for regex being used. It is also vulnerable to DOS attacks if it is server side and `word` comes from user input. Change `${word}` to use a properly escaped `word` instead (use the answer I linked in a comment on the question to see how to escape `word`). – Paul Aug 04 '19 at 20:12
0

Use this

const result = sentence.search(word) != -1;
Dominik Matis
  • 2,086
  • 10
  • 15
0

You could break it into words and then use Array.prototype.includes:

var sentence = 'The quick brown fox jumps over the lazy dog.';
var word = 'own';
console.log(`The word "${word}" ${sentence.match(/\b\S+\b/g).includes(word)? 'is' : 'is not'} in the sentence`);

sentence.match(/\b\S+\b/g) basically breaks your sentence in an array like:

["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]

And then includes just searches for the word there.

MinusFour
  • 13,913
  • 3
  • 30
  • 39
0

If the word is not coming in the start or end of the sentence you can just add space

var word = ' own ';

then you can get an exact match with same include function