Instance
The code is as follows |
Copy Code |
<script> var str= "yexj00"; Str=str.replace (/^s*|s*$/g, ""); alert (str); </script> |
Instance
Imitation RTrim:
The code is as follows |
Copy Code |
<script> var str= "yexj00"; Str=str.replace (/s*$/g, ""); alert (str); </script> |
Instance
The code and test code are as follows:
The code is as follows |
Copy Code |
< The script type= "Text/javascript" > //deletes white space characters on either side of the string. function Trim (str) { return str.replace (/^s+|s+$/g, '); } The //deletes the white space character to the left of the string. function LTrim (str) { return str.replace (/^s+/g, '); } The //deletes the white space character to the right of the string. function RTrim (str) { return str.replace (/s+$/g, '); } //The following is the test code var trimtest = "123456789"; There is a space before and after. document.write (' Length: ' +trimtest.length+ ' <br/> '); //before use document.write (' LTrim length: ' +ltrim (trimtest). length+ ' <br/> '); //Use LTrim after document.write (' RTrim length: ' +rtrim (trimtest). length+ ' <br/> '); //After using RTrim document.write (' Trim length: ' +trim (trimtest). length+ ' <br/> '); //after using trim </script> |
The results of the test are as follows:
The code is as follows |
Copy Code |
Length:11 LTrim length:10 RTrim Length:10 Trim Length:9 |