Check if string contains whitespace
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
Written by Angelos Chalaris
I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.
If you want to keep in touch, follow me on GitHub.