How can I check if string contains characters & whitespace, not just whitespace?

JavascriptStringWhitespace

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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
}

Solution 2 - Javascript

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
}

Solution 3 - Javascript

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

Solution 4 - Javascript

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

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

Solution 5 - Javascript

Just check the string against this regex:

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

Solution 6 - Javascript

if (!myString.replace(/^\s+|\s+$/g,""))
  alert('string is only whitespace');

Solution 7 - Javascript

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
}

Solution 8 - Javascript

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
}

Solution 9 - Javascript

This can be fast solution

return input < "\u0020" + 1;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpatadView Question on Stackoverflow
Solution 1 - JavascriptnickfView Answer on Stackoverflow
Solution 2 - JavascriptFullStackView Answer on Stackoverflow
Solution 3 - JavascriptPaul CreaseyView Answer on Stackoverflow
Solution 4 - JavascriptDaysonView Answer on Stackoverflow
Solution 5 - JavascriptIan ClellandView Answer on Stackoverflow
Solution 6 - JavascriptslimView Answer on Stackoverflow
Solution 7 - JavascriptWill StrohmeierView Answer on Stackoverflow
Solution 8 - JavascriptRajat SaxenaView Answer on Stackoverflow
Solution 9 - Javascript9meView Answer on Stackoverflow