Skip to content

Instantly share code, notes, and snippets.

@busseyl
Forked from detomon/strspn.js
Created October 13, 2018 14:54
Show Gist options
  • Save busseyl/66b6df7c467a31d2eef5d019d0a42cb1 to your computer and use it in GitHub Desktop.
Save busseyl/66b6df7c467a31d2eef5d019d0a42cb1 to your computer and use it in GitHub Desktop.
JavaScript implementation of the PHP function `strspn`
String.prototype.strspn = function (chars, start, length) {
var end;
if (start == undefined)
start = 0;
else if (start < 0)
start += this.length;
if (length == undefined)
length = this.length;
start = Math.max(0, Math.min(start, this.length));
end = Math.max(start, Math.min(start + length, this.length));
for (var i = start; i < end; i ++) {
if (chars.indexOf(this[i]) == -1)
return i - start;
}
return end - start;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment