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

patad picture patad · Jan 8, 2010 · Viewed 154.7k times · Source

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.

Answer

nickf picture nickf · Jan 8, 2010

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
}