Answers for "js test if string is only whitespace"

4

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)");
}
Posted by: Guest on May-17-2021
43

find whitespace in string js

var containsWhitespace = function(str){
	return /\s/g.test(s);
};
console.log(containsSpaces("foo bar"));//Returns true
console.log(containsSpaces("foobar"));//Returns false
Posted by: Guest on November-16-2021

Code answers related to "js test if string is only whitespace"

Code answers related to "Javascript"

Browse Popular Code Answers by Language