Q:

javascript trim spaces

var str=" I have outer spaces ";
var cleanStr=str.trim();//trim() returns string with outer spaces removed
25
const removeSpaces = str => str.replace(/\s/g, '');

// Example
removeSpaces('hel lo wor ld');      // 'helloworld'
1

 var str = "       Hello World!        ";
alert(str.trim());
 
2
value = value.trim();
5
var str = "       Hello World!        ";
alert(str.trim());
5
yourvariable.trim()
3
var str = '/var/www/site/Brand new document.docx';

document.write( 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
<div style="white-space:pre-wrap"> <!-- New Line -->
   <!-- 2 space --> This is my text
</div>
-1

New to Communities?

Join the community