Check if string contains whitespace

Angelos Chalaris · JavaScript, String, Regexp · Oct 18, 2020

Checks if the given string contains any whitespace characters.

  • Use RegExp.prototype.test() with an appropriate regular expression to check if the given string contains any whitespace characters.
const containsWhitespace = str => /\s/.test(str);
containsWhitespace('lorem'); // false
containsWhitespace('lorem ipsum'); // true

Recommended snippets

  • String is alphanumeric

    JavaScript, String

    Checks if a string contains only alphanumeric characters.

  • String is alpha

    JavaScript, String

    Checks if a string contains only alpha characters.

  • String is anagram

    JavaScript, String

    Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).