DEV Community

mhsohag11
mhsohag11

Posted on

Answer: How to detect string which contains only spaces?

To achieve this you can use a Regular Expression to remove all the whitespace in the string. If the length of the resulting string is 0, then you can be sure the original only contained whitespace. Try this:

var str = "    ";
if (!str.replace(/\s/g, '').length) {

Top comments (0)