1. js does not have trim functions, but you can write
Copy codeThe Code is as follows:
Function trim (str ){
Var newStr = str. replace (/^ \ s * $/g ,'')
Retrun newStr;
}
2. Remove spaces between the left and right sides of the string. trim, ltrim, or rtrim can be easily used in vbscript, but these three built-in methods are not available in js and need to be written manually. The following implementation method uses a regular expression, which is efficient and adds the three methods to the built-in methods of the String object.
The method format for writing a class is as follows: (str. trim ();)
Copy codeThe Code is 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>
You can write a function as follows: (trim (str ))
<Script type = "text/javascript">
Function trim (str) {// Delete spaces between the left and right sides
Return str. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
Function ltrim (str) {// Delete the left space
Return str. replace (/(^ \ s *)/g ,"");
}
Function rtrim (str) {// Delete the right space
Return str. replace (/(\ s * $)/g ,"");
}
</Script>