Skip to content

Instantly share code, notes, and snippets.

@Drozerah
Last active April 14, 2019 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drozerah/2b8e08d28413d66c3e63d7fce80994ce to your computer and use it in GitHub Desktop.
Save Drozerah/2b8e08d28413d66c3e63d7fce80994ce to your computer and use it in GitHub Desktop.
Return the frequency of a substring in a string
/**
* Return the frequency of a substring in a string
* @param {string} string - The string.
* @param {string} string - The substring to count.
* @returns {number} number - The frequency.
*
* @author Drozerah https://gist.github.com/Drozerah/2b8e08d28413d66c3e63d7fce80994ce
* @see https://stackoverflow.com/a/55670859/9370788
*/
const subStringCounter = (string, subString) => {
let count = 0
string.replace(new RegExp(subString, 'gi'), () => count++)
return count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment