167

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.

0

9 Answers 9

339

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

Just be careful with myString to be null value. It will return true: /\S/.test(null) == true
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?
@JonnyLeeds Since regex even has syntax support in js, one could say it's actually more native than any shipped utility methods ;)
74

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
}

1 Comment

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.
37
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 *.

Comments

23

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

if ($.trim(val).length === 0){
   // string is invalid
} 

2 Comments

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.
doesn't work when val is assigned spaces, in my case four spaces.
8

Just check the string against this regex:

if(mystring.match(/^\s+$/) === null) {
    alert("String is good");
} else {
    alert("String contains only whitespace");
}

1 Comment

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.
1
if (!myString.replace(/^\s+|\s+$/g,""))
  alert('string is only whitespace');

Comments

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
}

Comments

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
}

Comments

0

This can be fast solution

return input < "\u0020" + 1;

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.