Determine whether a string is "empty"
I need a JavaScript function to tell me whether a string object is empty. By "empty", I mean that it's not all just whitespace characters. I've written this prototype:
String.prototype.isEmpty = fun开发者_运维技巧ction() {
return this.length === 0 || this === " " || this.test(/^\s*$/);
}
Is this alright?
Is there a more-performant version of this out there?
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 a whitespace character: space, tab or new line.
\S will match non whitespace character:anything but not a space, tab or new line. If your string has a single character which is not a space, tab or new line, then it's not empty. Therefore you just need to search for one character: \S
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);
}
String.prototype.isEmpty = function() {
return this.length == 0 || /^\s*$/.test(this);
}
There is just a possibility out of 255 (not considering Unicode characters with code greater than 255) that a string with length 1 contains the space character. Considering strings with lenght greater than 1, the possibility get even lower.
Generally, using RegExp.prototype.test
for checking for a pattern match without compilation of a return array of matches (String.prototype.match
) likely has a better performance. I'd try -- but haven't tested 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 a side note, it is considered bad practice to fiddle with the prototype object of core javascript objects.
from 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 produce js error on IE 8
I fiddled with some of the things:
- I'm returning the inverse of this.length, for 0 that is
true
, for everything other number that isfalse
. - I removed the check for
" "
. I actually haven't seen much, if any cases where a single space was entered, removing the check is IMO faster on average. - And flipped the regex <-> string like the others did.
String.prototype.isEmpty = function() {
return !this.length || /^\s*$/.test(this);
}
Same answer as Daniel Vassallo originally provided, in example form.
String.prototype.isEmpty = function() {
return /^\s*$/.test(this);
}
document.body.style="background-color:black; color:green;font-size:12pt;font-family: 'Courier New', monospace;font-weight: bold";
String.prototype.isEmpty = function() {
return /^\s*$/.test(this);
}
let test0 = ''.isEmpty();
let test1 = '\xa0'.isEmpty();
let test2 = ' '.isEmpty();
let test3 = '\t'.isEmpty();
let test4 = '\n'.isEmpty();
let test5 = '\n\n'.isEmpty();
let test6 = '\n\t\n'.isEmpty();
let test7 = '\r\n'.isEmpty();
let test8 = '\r\t\n'.isEmpty();
let test9 = '\r\n\t'.isEmpty();
var texts = new Array(
("String.prototype.isEmpty = function() {"),
(" return /^\s*$/.test(this);"),
("}"),
(""),
("let test0 = ''.isEmpty() // " + test1),
("let test1 = ' '.isEmpty() // " + test2) + " (non breaking space)",
("let test2 = ' '.isEmpty() // " + test2),
("let test3 = '\\t'.isEmpty() // " + test3),
("let test4 = '\\n'.isEmpty() // " + test4),
("let test5 = '\\n\\n'.isEmpty() // " + test5),
("let test6 = '\\n\\t\\n'.isEmpty() // " + test6),
("let test7 = '\\r\\n'.isEmpty() // " + test7),
("let test8 = '\\r\\t\\n'.isEmpty() // " + test8),
("let test9 = '\\r\\n\\t'.isEmpty() // " + test9)
);
texts.forEach(text => {
let nobreakchar = '\xa0'
;
document.body.append(document.createTextNode(text.replaceAll(" ",nobreakchar)))
document.body.append(document.createElement("br"))
});
精彩评论