Building upon @Vittim.us answer above. I like the control his method gives me, making it easy to extend, but I needed to add case insensitivity and limit matches to whole words with support for punctuation. (e.g. "bath" is in "take a bath." but not "bathing")
The punctuation regex came from: https://stackoverflow.com/a/25575009/497745 (How can I strip all punctuation from a string in JavaScript using regex?)
function keywordOccurrences(string, subString, allowOverlapping, caseInsensitive, wholeWord){ string += ""; subString += ""; if (subString.length <= 0) return (string.length + 1); //deal with empty strings if(caseInsensitive) { string = string.toLowerCase(); subString = subString.toLowerCase(); } var n = 0, pos = 0, step = allowOverlapping ? 1 : subString.length, stringLength = string.length, subStringLength = subString.length; while (true) { pos = string.indexOf(subString, pos); if (pos >= 0) { var matchPos = pos; pos += step; //slide forward the position pointer no matter what if(wholeWord) //only whole word matches are desired { if(matchPos > 0) //if the string is not at the very beginning we need to check if the previous character is whitespace { if(!/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&\(\)*+,\-.\/:;<=>?@\[\]^_`{|}~]/.test(string[matchPos - 1])) //ignore punctuation { continue; //then this is not a match } } var matchEnd = matchPos + subStringLength; if(matchEnd < stringLength - 1) { if (!/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&\(\)*+,\-.\/:;<=>?@\[\]^_`{|}~]/.test(string[matchEnd])) //ignore punctuation { continue; //then this is not a match } } }++n; } else break; } return n;}
Please feel free to modify and refactor this answer if you spot bugs or improvements.