1 First:
2 String. Prototype. ltrim = Function () {
3Return This. Replace (/^ \ S */,"");
4} ; // Remove the left space;
5 String. Prototype. rtrim = Function () {
6Return This. Replace (/\ S * $/,"");
7} ; // Remove spaces on the right;
8 String. Prototype. Trim = Function () {
9Return This. Ltrim (). rtrim ();
10} ; // Remember that each sentence has a semicolon
11
12 Second:
13 Function Ltrim (STR) {
14 If (Str. charat ( 0 ) = "" ) {
15Str=Str. Slice (1);
16Str. ltrim ();
17}
18 Return STR;
19 }
20
21 Function Rtrim (STR) {
22 If (Str. charat (Str. Length - 1 ) = "" ) {
23Str=Str. Slice (0, Str. Length-1);
24Str. rtrim ();
25}
26 Return STR;
27 }
28
29 Function Trim (STR) {
30ReturnRtrim (ltrim (STR ));
31}
AboveCodeFor beginners to learn. Is the most basic function in JavaScript scripts.
The first kind of self-perception is better, and the prototype is adopted. Hope you can express your comments