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"
2
  • 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: stackoverflow.com/questions/3561493/… Commented Aug 4, 2019 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 ) Commented Aug 4, 2019 at 20:16

4 Answers 4

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")

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

7 Comments

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
@SanchezPanza what do you mean by that ? can you please elaborate ?
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?)
Just note that this will also match things like: " ", ".", "" and "quick brown".
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).
|
0

Use this

const result = sentence.search(word) != -1;

3 Comments

Why should he do that?
@Rob to get true or false
How does that happen?
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.

1 Comment

That works, buts becomes heavy if there is an array of strings imho?
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

Comments

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.