Davyd
0
Q:

trim whitespace javascript

var str=" I have outer spaces ";
var cleanStr=str.trim();//trim() returns string with outer spaces removed
25
var str = "  Some text ";
str.trim();
4

 var str = "       Hello World!        ";
alert(str.trim());
 
2
value = value.trim();
5
yourvariable.trim()
3
/*
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