10

In my code i need to write an if else block-

when the variable `currentValue` is holding only spaces -> certain code

But i don't know how to write this condition as currentValue can be a string of any size. It can hold " ", " " etc. if i write currentValue!=" " it checks for single space.

2
  • do you mean spaces (char 32) precisely, or any whitespace (end of line, tab etc)? Commented Jul 8, 2013 at 10:35
  • You should probably try Googling for something like javascript match string. Commented Jul 8, 2013 at 10:37

4 Answers 4

25

Could look like

if( !currentValue.trim().length ) {
    // only white-spaces
}

docs: trim

Even if its very self-explanatory; The string referenced by currentValue gets trimmed, which basically means all white-space characters at the beginning and the end will get removed. If the entire string consists of white-space characters, it gets cleaned up alltogether, that in turn means the length of the result is 0 and !0 will be true.

About performance, I compared this solution vs. the RegExp way from @mishik. As it turns out, .trim() is much faster in FireFox whereas RegExp seems way faster in Chrome.

http://jsperf.com/regexp-vs-trim-performance

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe it's because of lack of support in IE < 9, perhaps. Who knows. It's simple to shim String.prototype.trim though, and even simpler to write a comment explaining the downvote.
The code is perfect, but does not "function right" in case of zero length string. So, if string contain no spaces at all, and the value is "", then it may be a complete different case. so: if( !"".trim().length ) { /* white spaces AND zero length string */ }
@Armen that might be a meaningful distinction, but an empty string does contain only space characters. That is, every character in an empty string is a space character.
9

Simply:

if (/^\s*$/.test(your_string)) {
  // Only spaces
}

To match space only:

if (/^ *$/.test(your_string)) {
  // Only spaces
}

Explanation: /^\s*$/ - match a beginning of a string, then any number of whitespaces (space, newline, tab, etc...), then end of string. /^ *$/ - same, but only for spaces.

If you do not want to match empty string: replace * with + to make sure that at least one character is present.

1 Comment

This matches the empty string too, and also newlines, and tabs.
0

One can check if the string does not contain any non-whitespace characters with a regular expression. This method will only check each character at most once and will exit early as soon as it encounters a character that is not whitespace.

if(!/\S/.test(str)){
   console.log('str contains only whitespace');
}

One could also use String#trim to remove all whitespace from the beginning and end of the string. If the string only contained whitespace, the result will be an empty string, which is falsy.

if(!str.trim()){
    console.log('str contains only whitespace');
}

If the string might be null or undefined, the optional chaining operator can be used.

if(!str?.trim()){
    console.log('str is null or undefined, or contains only whitespace');
}

Comments

-1

try-:

your_string.split(" ").length

EDIT:

var your_string = "               ";
var x = your_string.split(" ").length - 1;

if ( your_string.length > 0 && (your_string.length - x) == 0 ) {
    alert("your_string has only spaces");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.