1

I want to check whether a word exists in a string. I tried to use search() function, but it also counts the words containing the searched word. For example, let my string be

var str = "This is a pencil.";

When I search for "is"

str.search("is");

gives 2 while I want to get 1 which should be only "is", not "This". How can I achieve this?

Thanks in advance...

0

3 Answers 3

4

Well, it seems that you want to search for entire words, not strings.

Here's one approach that should suffice (although isn't fullproof).

Split the string into tokens based on whitespace or punctuation, this should give you a list of words (with some possible empty strings).

Now, if you want to check if your desired word exists in this list of words, you can use the includes method on the list.

console.log(
  "This is a pencil."
    .split(/\s+|\./) // split words based on whitespace or a '.'
    .includes('is')  // check if words exists in the list of words
)

If you want to count occurrences of a particular word, you can filter this list for the word you need. Now you just take the length of this list and you'll get the count.

console.log(
  "This is a pencil."
    .split(/\s+|\./) // split words based on whitespace or a '.'
    .filter(word => word === 'is')
    .length
)

This approach should also handle words at the beginning or the end of the string.

console.log(
  "This is a pencil."
    .split(/\s+|\./) // split words based on whitespace or a '.'
    .filter(word => word === 'This')
    .length
)

Alternatively, you could also reduce the list of words into the number of occurences

console.log(
  "This is a pencil."
    .split(/\s+|\./) // split words based on whitespace or a '.'
    .reduce((count, word) => word === 'is' ? count + 1 : count, 0)
)

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

5 Comments

You can just do a .indexOf() rather than a filter after you split, can't you?
Also won't match "pencil" because the element will be "pencil." won't it?
@mhodges I don't think so unless I misunderstood the question. OP wants to count occurrences and indexOf returns the index of the first occurrence. As far as not matching pencil, I've mentioned this isn't a full proof solution. A full proof solution for this might be beyond the scope of an SO question. Neverthless, I've included . specifically for this use-case.
Hm, you may be right. At first it says "check whether a word exists in a string", but later in the post says they want to return 1, not 2. +1 regardless
@mhodges yeah, the wording is ambiguous, I've added a use-case for the existence check as well, thanks for the suggestion.
2

search() takes a regex, so you can search for is surrounded by spaces

alert(str.search(/\sis\s/))

1 Comment

What if the word is in the beginning or at the end? Is it still valid?
0

You can still use your .search() or .indexOf() to find the words. When looking for "is" in the "This is a pencil." example just do a str.search(" is ") (note the space before and after the word).

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.