Created
May 9, 2020 13:59
-
-
Save urain39/a85e9cdb2fa940fc801923ac35046811 to your computer and use it in GitHub Desktop.
Python-like substring count method for JavaScript String prototype.
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
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