It looks like you need to use /^\s*$/.test(this)
instead of this.test(/^\s*$/)
. There is no test()
method for strings, unless you're using some JavaScript library that implements this method.
The /^\s*$/.test(this)
would have been enough, but the first two expressions would short circuit if any one of them evaluates to true, without having to test the regular expression. That should be pretty efficient.
As @Matthew Crumley noted in a comment above, your this === " "
expression will always evaluate to false. You could remove this expression, or use ==
if you would will be expecting a lot of strings with just a single space character:
String.prototype.isEmpty = function() {
return this.length === 0 || this == " " || /^\s*$/.test(this);
}