Skip to content

Instantly share code, notes, and snippets.

@urain39
Created May 9, 2020 13:59
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 urain39/a85e9cdb2fa940fc801923ac35046811 to your computer and use it in GitHub Desktop.
Save urain39/a85e9cdb2fa940fc801923ac35046811 to your computer and use it in GitHub Desktop.
Python-like substring count method for JavaScript String prototype.
String.prototype.count = function(substring) {
var i = -1,
count = 0;
if (substring)
while ((i = this.indexOf(substring, i + 1)) !== -1)
count++;
else
count = this.length + 1;
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment