Check If A String Contains Only Spaces In JavaScript

To check if a string contains only spaces in JavaScript we can use two methods –

  1. Use trim() method to remove all spaces from both sides of a string and then check if the string is empty or not.
  2. Use test() method with regular expression /^\s*+$/ to check if string contains only spaces.

Let’s have a look at these methods in detail.

Check If A String Contains Only Spaces In JavaScript

check if a string contains only spaces in javascript

1) Using trim() Method

The trim() method removes whitespace from both sides of a string. So if we have a string with only spaces, then after using the trim() method, it will become an empty string. And we can check if the string is empty or not using the length property.

If the length of the trimmed string is 0, then it means the string contains only spaces.

Example:

var str = ” “; // string with only spaces

if(str.trim().length == 0){

  console.log(“String contains only spaces”);

}else{

  console.log(“String does not contain only spaces”);

}

You can write a reusable function to check if a string contains only spaces or not using the trim() method.

function checkSpaces(str){

if(str.trim().length == 0){

  return true;

}else{

  return false;

}

}

console.log(checkSpaces(” “)); //true

console.log(checkSpaces(” abc “)); //false

console.log(checkSpaces(“”)); //true

2) Using test() Method With Regular Expression /^\s*$/

We can also use the test() method with a regular expression to check if the string contains only spaces. The regular expression /^\s*$/ matches any number of white-space characters (spaces, tabs, newlines) at the beginning and end of a string.

If this regular expression matches the given string, then it means that the given string contains only spaces.

Regexp.test(String)

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

Syntax:

regexp.test(str)

Parameter: This function accept a mandatory parameter str which specify the string to search.

Return value: This function returns true if a match is found, otherwise it returns false.

Example:

var str = ” “; // string with only spaces

if(/^\s*$/.test(str)){

  console.log(“String contains only spaces”);

}else{

  console.log(“String does not contain only spaces”);

}

In the above method, the / character is an escape character so we have to use \s instead of s.

The ^ character matches the starting position of the string.

The $ character matches the ending position of the string.

The * character matches any number of characters (including zero characters).

So /^\s*$/ will match a string which contains only spaces and nothing else.

The if statement will check if the test() method return true or false. If it returns true, then it means the given string contains only spaces and if it returns false, then it means the given string does not contain only spaces.

You can write a reusable function to check if a string contains only spaces or not.

function isStringOnlySpace(str){

  return /^\s*$/.test(str);

}

console.log(isStringOnlySpace(” “)); // true

console.log(isStringOnlySpace(“abcd”)); // false

console.log(isStringOnlySpace(“”)); // true

console.log(isStringOnlySpace(” a b c d “)); // false

Note that empty string also passes the above test as it contains only spaces.

And that’s how you can check if a string contains only spaces in JavaScript.

I hope this helped!

Leave a Comment