From jQuery JavaScript Library
« Back to Utilities
jQuery.trim( str )
Remove the whitespace from the beginning and end of a string.
Uses a regular expression to remove whitespace from the given string.
Arguments:| str | String | |
|---|
| The string to trim. |
Examples:| Name | Type |
Removes the two whitespaces at the start and at the end of the string.
$("button").click(function () {
var str = " lots of spaces before and after ";
alert("'" + str + "'");
str = jQuery.trim(str);
alert("'" + str + "' - no longer");
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="../jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("button").click(function () {
var str = " lots of spaces before and after ";
alert("'" + str + "'");
str = jQuery.trim(str);
alert("'" + str + "' - no longer");
});
});
</script>
</head>
<body>
<button>Show Trim Example</button>
</body>
</html>
Removes the two whitespaces at the start and at the end of the string.
$.trim(" hello, how are you? ");