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.