Determine if the string is "empty"

I need a JavaScript function to tell me if a string object is empty. By "empty" I mean that these are not all just whitespace characters. I wrote this prototype:

String.prototype.isEmpty = function() { return this.length === 0 || this === " " || this.test(/^\s*$/); } 

It's good?

Is there a more efficient version of this?

+7
javascript regex
source share
6 answers

Use

 String.prototype.isEmpty = function() { if (!this.match(/\S/)) { return ('enter some valid input.Empty space is not allowed'); } else { return "correct input"; } } alert("test 1:"+(" ".isEmpty())); alert("test 2:"+(" \t ".isEmpty())); alert("test 3:"+(" \n ".isEmpty())); alert("test 4:"+("hi".isEmpty())); 

Note:

\ s will match whitespace: space, tab or newline.

\ S will match a character without spaces: nothing but a space, tab, or newline. If your line has one character, which is not space, tab or new line, then it is not empty. So you just need to look for one character: \ S

+12
source share

It looks like you need to use /^\s*$/.test(this) instead of this.test(/^\s*$/) . There is no test() method for strings unless you use any JavaScript library that implements this method.

/^\s*$/.test(this) would be enough, but the first two expressions would be short if either of them evaluates to true without experiencing a regular expression. This should be pretty effective.

Like @Matthew Crumley noted in the comment above, your expression this === " " will always be evaluated as false. You can delete this expression or use == if you would expect many lines with one space character:

 String.prototype.isEmpty = function() { return this.length === 0 || this == " " || /^\s*$/.test(this); } 
+4
source share
 String.prototype.isEmpty = function() { return this.length == 0 || /^\s*$/.test(this); } 

There is only the possibility of 255 (not counting Unicode characters with a code greater than 255) that a line with a length of 1 contains a whitespace character. Considering lines with a length greater than 1, the possibility becomes even lower.

+1
source share

Generally, using RegExp.prototype.test to check for pattern matching without compiling the returned match array ( String.prototype.match ) probably has better performance. I would try - but have not tested it yet - something like:

 function isEmpty() { var string = arguments[0] ; var EMPTY_STRING_PATTERN = /^\s*$/ , empty = false ; if( EMPTY_STRING_PATTERN.exec(string) === true ) empty = true ; return empty ; } 

On the side of the note, it is considered bad practice to mess with the prototype object of the main javascript objects.

+1
source share

I was looking for some things:

  • I am returning the inversion of this.length, for 0, which is true , for all another number that is false .
  • I deleted the check for " " . I actually have not seen much, if in any cases when one place was introduced, the removal of the check on average is IMO.
  • And he turned over the line of the regular expression ↔, as others did.

String.prototype.isEmpty = function() {
return !this.length || /^\s*$/.test(this);
}

0
source share

from the example https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim

 var orig=" foo " document.write(orig.trim()) // foo document.write(orig.trim().length) // 3 

tested on FF 3.6.8, Safari 5.0.1, Google Chrome 5.0.375.127, but generates js error in IE 8

0
source share

All Articles