If your project is using the jQuery framework, you will choose jQuery. trim (string) to remove the null characters at the beginning and end of the string ). If jQuery is not used, how can we implement it easily. I have previously sent a small code snippet: Javascript removes spaces around the string-trim (). This write is rough and requires a pass operation. So many people are not satisfied with the quality of the Code, including me.
I occasionally read jQuery code and find that it is very valuable for us to learn.
Let's see how it implements this function. The Code has only one sentence: use the regular expression method.
JavaScript Trim implementation code
Function trim (text ){
Return (text | ""). replace (/^/s + |/s + $/g ,"");
}
If jQuery or other frameworks are not used in your project, you can use this function to Copy the code. I believe that the structure and quality of the Code will satisfy you.