There is no trim function in the 1.js itself, but you can write a
Copy Code code as follows:
function Trim (str) {
var newstr = str.replace (/^\s*$/g, "")
Retrun Newstr;
}
2. Remove the space at both ends of the string, in VBScript you can easily use trim, LTrim, or RTrim, but in JS there is no such 3 built-in methods, need to hand-write. The following implementation method uses regular expressions and is efficient and adds these three methods to the built-in methods of the string object.
The methods written in the class are as follows: (Str.trim ();)
Copy Code code as follows:
<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 can be written as a function: (Trim (str))
<script type= "Text/javascript" >
function Trim (str) {//Remove spaces at left and right ends
Return Str.replace (/(^\s*) | ( \s*$)/g, "");
}
function LTrim (str) {//delete left space
Return Str.replace (/(^\s*)/g, "");
}
function RTrim (str) {//Remove the right space
Return Str.replace (/(\s*$)/g, "");
}
</script>