Q:

remove whitespace javascript

var spacesString= "Do I have spaces?"; 
var noSpacesString= myString.replace(/ /g,'');// "DoIhavespaces?"
9
var str = "  Some text ";
str.trim();
4
if (/\s/.test(str)) {
    // It has any kind of whitespace
}
2
//Removing whitespace from String
 var str = "       Hello World!        ";
console.log(str.trim());
 
4

 var str = "       Hello World!        ";
alert(str.trim());
 
2
return str.replace(/\s/g, '');
0
/*
For browsers that do not support the trim() method, 
you can remove whitespaces from both sides of a string with a regular expression:
*/
 function myTrim(x) {
  return x.replace(/^\s+|\s+$/gm,'');
}


 function myFunction() {
  var str = myTrim("        Hello World!        ");
  alert(str);

 }

 
0

New to Communities?

Join the community