1

I am trying to build a function that will count the number of occurrences of a word in a phrase.

The function should include cases where the word in the phrase has additional non-alphabetical characters and/or end-of-line characters.

function countWordInText(word,phrase){
    var c=0;
    phrase = phrase.concat(" ");
    regex = (word,/\W/g);
    var fChar = phrase.indexOf(word);
    var subPhrase = phrase.slice(fChar);

    while (regex.test(subPhrase)){
        c += 1;
        subPhrase = subPhrase.slice((fChar+word.length));
        fChar = subPhrase.indexOf(word);
    }
    return c;
}

The problem is that for a simple values, such as

phrase = "hi hi hi all hi. hi";
word = "hi"
// OR
word = "hi all";

it returns false values.

Adaline Simonian
  • 4,076
  • 1
  • 22
  • 35
sale108
  • 457
  • 6
  • 17

1 Answers1

1

The algorithm you wrote shows you spent some time trying to get this to work. However, there's still quite a few places where it won't work. For example, (word,/W/g) isn't actually creating the regex you might think it is.

There's also a much simpler way:

function countWordInText (word, phrase) {
  // Escape any characters in `word` that may have a special meaning
  // in regular expressions.
  // Taken from https://stackoverflow.com/a/6969486/4220785
  word = word.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')

  // Replace any whitespace in `word` with `\s`, which matches any
  // whitespace character, including line breaks.
  word = word.replace(/\s+/g, '\\s')

  // Create a regex with our `word` that will match it as long as it
  // is surrounded by a word boundary (`\b`). A word boundary is any
  // character that isn't part of a word, like whitespace or
  // punctuation.
  var regex = new RegExp('\\b' + word + '\\b', 'g')

  // Get all of the matches for `phrase` using our new regex.
  var matches = phrase.match(regex)

  // If some matches were found, return how many. Otherwise, return 0.
  return matches ? matches.length : 0
}

countWordInText('hi', 'hi hi hi all hi. hi') // 5

countWordInText('hi all', 'hi hi hi all hi. hi') // 1

countWordInText('hi all', 'hi hi hi\nall hi. hi') // 1

countWordInText('hi all', 'hi hi hi\nalligator hi. hi') // 0

countWordInText('hi', 'hi himalayas') // 1

I put comments throughout the example. Hopefully this helps you get started!

Here are a few great places to learn about regular expressions in Javascript:

You can also test your regular expressions live with Regexr.

Adaline Simonian
  • 4,076
  • 1
  • 22
  • 35
  • what can I say man? I struggled with that for hours yesterday. I don't fully understand the code, but the referrals would be a great help! thank you so much! – sale108 Jul 29 '17 at 09:51