166

What is the best way to check if a string contains only whitespace?

The string is allowed to contain characters combined with whitespace, but not just whitespace.

ckittel
  • 6,478
  • 3
  • 41
  • 71
patad
  • 9,364
  • 11
  • 38
  • 44

9 Answers9

336

Instead of checking the entire string to see if there's only whitespace, just check to see if there's at least one character of non whitespace:

if (/\S/.test(myString)) {
    // string is not empty and not just whitespace
}
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 10
    Just be careful with myString to be null value. It will return true: /\S/.test(null) == true – Dilshod Tadjibaev Jan 05 '14 at 08:13
  • 7
    a lot of these answers have regex in them! Does this mean there is no native way to detect thing in js? is there no string.IsWhitespace or something? Is there no native trim either? – JonnyRaa Feb 06 '14 at 17:08
  • 8
    @JonnyLeeds Since regex even has syntax support in js, one could say it's actually more native than any shipped utility methods ;) – Ricardo van den Broek May 29 '17 at 15:17
70

Simplest answer if your browser supports the trim() function

if (myString && !myString.trim()) {
    //First condition to check if string is not empty
    //Second condition checks if string contains just whitespace
}
FullStack
  • 5,902
  • 4
  • 43
  • 77
  • 1
    Nowadays when IE 8 is RIP, this is the cleanest and most efficient solution performance-wise relying on the native implementation. And it works correctly with tabs and new lines. – Alexander Abakumov May 24 '18 at 15:26
38
if (/^\s+$/.test(myString))
{
      //string contains only whitespace
}

this checks for 1 or more whitespace characters, if you it to also match an empty string then replace + with *.

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
23

Well, if you are using jQuery, it's simpler.

if ($.trim(val).length === 0){
   // string is invalid
} 
epicwhale
  • 1,835
  • 18
  • 26
  • 2
    Works for newline and tab too, whereas the regex examples above don't as they are only looking for the absence of anything but white space. Although, I'm sure someone with some regex knowledge could create a regex that would also include tab / newline in the search. – Kate Apr 26 '16 at 10:49
  • doesn't work when val is assigned spaces, in my case four spaces. – user1451111 Aug 02 '17 at 07:41
8

Just check the string against this regex:

if(mystring.match(/^\s+$/) === null) {
    alert("String is good");
} else {
    alert("String contains only whitespace");
}
Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
  • 1
    The way I read the question, is says that /any/ whitespace is allowed, as long as the string isn't /only/ whitespace. It is silent on what to do if the string is empty, so it may be that nickf's answer is still better. – Ian Clelland Jan 11 '10 at 18:19
1
if (!myString.replace(/^\s+|\s+$/g,""))
  alert('string is only whitespace');
slim
  • 2,545
  • 1
  • 24
  • 38
1

I've used the following method to detect if a string contains only whitespace. It also matches empty strings.

if (/^\s*$/.test(myStr)) {
  // the string contains only whitespace
}
Rajat Saxena
  • 3,834
  • 5
  • 45
  • 63
0

The regular expression I ended up using for when I want to allow spaces in the middle of my string, but not at the beginning or end was this:

[\S]+(\s[\S]+)*

or

^[\S]+(\s[\S]+)*$

So, I know this is an old question, but you could do something like:

if (/^\s+$/.test(myString)) {
    //string contains characters and white spaces
}

or you can do what nickf said and use:

if (/\S/.test(myString)) {
    // string is not empty and not just whitespace
}
Community
  • 1
  • 1
0

This can be fast solution

return input < "\u0020" + 1;
9me
  • 1,078
  • 10
  • 36
  • That's just doing `return input < " 1";` Which is just doing alphabetical compare. As long as input is sorted lower than " 1", it will return true. Example: `return " asdfv34562345" < "\u0020" + 1;` evaluates to true. – Derek Ziemba May 05 '20 at 17:50