Skip to content

Instantly share code, notes, and snippets.

@detomon
Created March 28, 2014 10:49
Show Gist options
  • Save detomon/9830030 to your computer and use it in GitHub Desktop.
Save detomon/9830030 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;
};
Copy link

ghost commented May 3, 2017

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment