grammar
string.trim()
Parameter Value
no
return value
Type: string Description: Returns a string of trailing spaces removed
technical details
JavaScript version: ECMAScript 5
Removal spaces left and right ends of the string, can trim, ltrim or rtrim in vbscript inside, but did not in the js built these three methods, the preparation requires manual. The following implementation uses regular expressions, good efficiency, and these three method of adding a built-in method of the String object to.
Method format written in the following categories: (str.trim ();)
<script language= "javascript>
String.prototype.trim= function(){
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function(){
return this.replace(/(^\s*)/g,"");
}
String.prototype.rtrim = function(){
return this.replace(/(\s*$)/g,"");
}
</script>
This function can be written as: (trim ())
<script type="text/javascript">
function trim (str) {// delete the spaces at both ends of the left and right
return str.replace(/(^\s*)|(\s*$)/g,"");
}
function ltrim (str) {// remove the spaces left / head
return str.replace(/(^\s*)/g,"");
}
function rtrim (str) {// Remove the space to the right / tail
return str.replace(/(\s*$)/g,"");
}
</script>