Last active
April 14, 2019 00:01
-
-
Save Drozerah/2b8e08d28413d66c3e63d7fce80994ce to your computer and use it in GitHub Desktop.
Return the frequency of a substring in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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