Categories
JavaScript Answers

How to check if string contains characters and whitespace with JavaScript?

Spread the love

Sometimes, we want to check if string contains characters and whitespace with JavaScript.

In this article, we’ll look at how to check if string contains characters and whitespace with JavaScript.

How to check if string contains characters and whitespace with JavaScript?

To check if string contains characters and whitespace with JavaScript, we check if there’re non-whitespace characters in the string.

For instance, we write

if (/\S/.test(myString)) {
  // ...
}

to call /\S/.test with myString to check if myString has any non-whitespace characters.

Conclusion

To check if string contains characters and whitespace with JavaScript, we check if there’re non-whitespace characters in the string.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *