Syntax
string.trim()
- The trim() method removes whitespace from both sides of a string.
- The trim() method does not change the original string.
Example1
1 var str = " Hello World! ";
2 alert(str.trim());
Example2
1 function myTrim(x) {
2 return x.replace(/^s+|s+$/gm,'');
3 }
4
5 function myFunction() {
6 var str = myTrim(" Hello World! ");
7 alert(str);
8 }
- Remove the whitespace from the beginning and end of a string.
Syntax
jQuery.trim( str )
Example
1 <script>
2 var str = " lots of spaces before and after ";
3 $( "#original" ).html( "Original String: '" + str + "'" );
4 $( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" );
5 </script>