The Wayback Machine - https://web.archive.org/web/20240213201615/https://www.geeksforgeeks.org/javascript-program-to-check-if-a-string-contains-any-whitespace-characters/
Open In App
Related Articles

JavaScript Program to Check if a String Contains any Whitespace Characters

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we are given a string str, you need to find whether the given string contains whitespace characters or not in JavaScript. Whitespaces can occur at any position whether at the beginning or in between or at the end of the string.

Example 1:

Input:
str = Hi I am a Geek
Output:
The string contains whitespace characters
Explanation:
In the input string "Hi I am a geek" there are 4 whitespace characters which is shown by "_" :
Hi_I_am_a_Geek
So we return "The string contains whitespace characters"

Example 2:

Input:
str = helloWorld
Output:
The string does not contains whitespace characters

Approaches to check if a string contains any whitespace characters

  • Using regular expressions
  • Using JavaScript Sets

Approach 1: Using regular expressions

To determine whether a string contains whitespace characters we can make use of regular expressions. A regular expression (regex) is a sequence of characters that define a search pattern. The “\s” metacharacter is used to match the whitespace character.

Example: Below is the implementation of the approach

Javascript




function checkWhitespace(str) {
    return /\s/.test(str);
}
let str = "Hi I am a Geek";
if (checkWhitespace(str)) {
    console.log(
        "The string contains whitespace characters."
    );
} else {
    console.log(
        "The string does not contain whitespace characters."
    );
}


Output

The string contains whitespace characters.

Approach 2: Using JavaScript Sets

  • Create a new Set which contains whitespace characters i.e. space, \n, \t.
  • Iterate over the string and check whether the set contains any character of the string in it.
  • If it contains any character of the string it means that the string contains whitespace characters.
  • Return true else return false.

Example: Below is the implementation of the approach

Javascript




function checkWhitespace(str) {
    let whitespace = new Set([" ", "\t", "\n"]);
    for (let i = 0; i < str.length; i++) {
        if (whitespace.has(str[i])) {
            return true;
        }
    }
    return false;
}
  
let str = "GeeksforGeeks";
if (checkWhitespace(str)) {
    console.log(
        "The string contains whitespace characters."
    );
} else {
    console.log(
        "The string does not contain whitespace characters."
    );
}


Output

The string does not contain whitespace characters.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 18 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials