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.