check if a string contains a space javascript code example
Example 1: how to check if a string contains only spaces in javascript
let str = " ";
if (!str.replace(/\s/g, "").length) {
console.log("string only contains whitespace (ie. spaces, tabs or line breaks)");
}
Example 2: check whitespace in javascript
if (/\s/.test(str)) {
// It has any kind of whitespace
}
Example 3: js find space in string
if(str.indexOf(' ') >= 0){
console.log("contains spaces");
}