-1

I'm familiar with searching a string for a given substring:

if (string.indexOf(substring) > -1) {
    var containsSubstring = true;
}

But what if the substring needs to be a word?

A word:

  • it must be at the beginning of the string with a space after it; or
  • at the end of the string with a space before it; or
  • in the middle of the string with a space on each side

If I'm looking for the substring fox:

the quick brown fox // matches
fox jumps over the lazy dog // matches
quick brown fox jumps over // matches


the quick brownfox // does not match
foxjumps over the lazy dog // does not match
quick brownfox jumps over // does not match
quick brown foxjumps over // does not match
quick brownfoxjumps over // does not match

Is there any way to achieve the results above with indexOf or will I need to use regex?

Community
  • 1
  • 1
Rounin
  • 27,134
  • 9
  • 83
  • 108
  • 2
    What is the difference between a substring and a word in that context? – Y2H Aug 30 '17 at 15:34
  • Have you considered using regex? Or jQuery – evolutionxbox Aug 30 '17 at 15:35
  • Regex is probably the cleanest, but using a space would work if you check all the coditions – i-man Aug 30 '17 at 15:37
  • Possible duplicate of [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – Surreal Aug 30 '17 at 15:38
  • Sorry, everyone, I thought _a word_ (as in regex `\w`) was self-explanatory, but it wasn't, so I have added an explanation above of what I mean by _a word_ . Obviously I'm not just looking for a substring in a string - I did begin the question by explaining that I'm familiar with such a process ;-) – Rounin Aug 30 '17 at 16:04
  • Can someone explain the downvote, please? Thank you. – Rounin Aug 30 '17 at 16:34

3 Answers3

1

You can use the search method.

var string = "foxs sas"

var search = string.search(/\bfox\b/) >= 0? true : false;

console.log(search)
Rafael Umbelino
  • 771
  • 7
  • 14
  • Nope. The method above returns `true` every time the substring `fox` is present in the string - even when the substring `fox` is not a word. For instance `brownfox jumps` and `brown foxjumps` and `brownfoxjumps` will all still return `true`. – Rounin Aug 30 '17 at 16:12
  • look again please! – Rafael Umbelino Aug 30 '17 at 16:27
1

Have you tried using a regex with word boundaries:

if (/(\bfox\b)/g.test(substring)) {
    var containsSubstring = true;
}

https://regex101.com/r/G3iOGi/1

combatc2
  • 1,215
  • 10
  • 10
  • @Rounin if this is the correct answer can you mark it please, I need reputation :) – combatc2 Aug 30 '17 at 17:02
  • Excellent work, @combatc2 - thank you. I've been away from my laptop, otherwise I'd have marked it sooner. – Rounin Aug 30 '17 at 18:13
  • I have separated it out by putting the regex in its own variable: `var pattern = /\bfox\b/g; var string = 'thequickbrown fox'; console.log(pattern.test(string));` `.test()` is perfect, because it returns a boolean value without requiring an if statement - thanks again. – Rounin Aug 30 '17 at 18:25
0

You can achieve this by checking if it is at the start of the string and has a space after, or is at the end of the string and has a space before, or is in the middle of the string and has a space before and after.

Frank Egan
  • 146
  • 8
  • Thanks Frank. Yes I understand what I need to do. I just don't understand how I can use `indexOf()` to achieve this. – Rounin Aug 30 '17 at 16:08
  • So... it turns out that while `indexOf()` is fine for plain substrings, `search()` is the method to use for regular expressions. Though, in this specific instance, the `test()` method, mentioned by @combatc2 above, is the best solution of all. – Rounin Aug 30 '17 at 19:42