To achieve a
1.String.prototype.trim = function () (
2. Return this.replace (/ ^ \ s \ s * /,''). Replace (/ \ s \ s * $ /,'');
3.)
Looks less like to spend two regular replacement, the actual speed is amazing, mainly thanks to the internal optimization of the browser. A well-known example of a string splicing, direct additive made of StringBuffer than using Array faster. base2 library to use this implementation.
Achieve two
1.String.prototype.trim = function () (
2. Return this.replace (/ ^ \ s + /,''). Replace (/ \ s + $ /,'');
3.)
And the achievement of a very similar, but slower that the main reason is that it first assumes that there exists at least one blank character. Prototype.js use of this implementation, but its name as the strip, because the method of Prototype and Ruby are all seeking the same name.
Achieve three
1.String.prototype.trim = function () (
2. Return this.substring (Math.max (this.search (/ \ S /), 0), this.search (/ \ S \ s * $ /) + 1);
3.)
To intercept obtained a blank part of the (of course, allow the existence of gaps in the middle of character), a total of four native method call. Very cleverly designed, substring with two numbers as a parameter. Math.max with two numbers as a parameter, search returns a number. Faster than the above two bit slower, but faster than the majority of the following.
Achieve 4
1.String.prototype.trim = function () (
2. Return this.replace (/ ^ \ s + | \ s + $ / g,'');
3.)
This can be described as a simplified version of realization of two, is the use of the candidate operator to connect two regular. But doing so will lose the opportunity to optimize the browser, not as to achieve 3. Since it appears that a very elegant, many libraries are using it, such as JQuery and mootools
Achieving 5
1.String.prototype.trim = function () (
2. Var str = this;
3. Str = str.match (/ \ S + (?: \ S + \ S +)*/);
4. Return str? Str [0]:'';
5.)
match is to return an array, so the original strings to meet the requirements it has become part of the element. In order to prevent gaps in the middle of the string characters are excluded, we need to capture the nature of the use to non-group (?: Exp). Since the array may be empty, we later had to be further determined. In dealing with groups like the browser is relatively weak, a word slowly. So do not superstition is regular, although it is basically a panacea.
Achieving 6
1.String.prototype.trim = function () (
2. Return this.replace (/ ^ \ s * (\ S * (\ s + \ S +) *) \ s * $ /, '$ 1');
3.)
To meet the requirements of the section provides out into an empty string. However, poor efficiency, especially in the IE6.
Achieve 7
1.String.prototype.trim = function () (
2. Return this.replace (/ ^ \ s * (\ S * (?: \ S + \ S +) *) \ s * $ /, '$ 1');
3.)
And the achievement of six very similar, but with a non-capturing group carried out the advantages of performance enhancing effect of a little bit.
Achieve 8
1.String.prototype.trim = function () (
2. Return this.replace (/ ^ \ s *((?:[ \ S \ s] * \ S)?) \ S * $ /, '$ 1');
3.)
Along the above two modes of thinking to improve, using a non-capturing group with the character set, use? Replace the *, the effect is very striking. Especially in IE6, you can use to describe this insane performance improvements, direct spike Firefox.
Achieving 9
1.String.prototype.trim = function () (
2. Return this.replace (/ ^ \ s * ([\ S \ s] *?) \ S * $ /, '$ 1');
3.)
This is replaced with the lazy, non-capturing group matches, have been improved in Firefox, IE is not so crazy last time.
Achieve 10
01.String.prototype.trim = function () (
02. Var str = this,
03. Whitespace = '\ n \ r \ t \ f \ x0b \ xa0 \ u2000 \ u2001 \ u2002 \ u2003 \ u2004 \ u2005 \ u2006 \ u2007 \ u2008 \ u2009 \ u200a \ u200b \ u2028 \ u2029 \ u3000';
04. For (var i = 0, len = str.length; i <len; i + +) (
05. If (whitespace.indexOf (str.charAt (i)) === -1) (
06. Str = str.substring (i);
07. Break;
08.)
09.)
10. For (i = str.length - 1; i> = 0; i -) (
11. If (whitespace.indexOf (str.charAt (i)) === -1) (
12. Str = str.substring (0, i + 1);
13. Break;
14.)
15.)
16. Return whitespace.indexOf (str.charAt (0)) === -1? Str:'';
17.)
I just want to say that people no longer come up with cattle to describe is the same as the level of God. It first put a blank character could list them all in the first traversal of cut in front of a blank, the second cut behind the gaps. The whole process took only indexOf and substring of this string is born specifically to deal with the native method is not used to the regular. Speed is amazing, it is estimated almost equal to the binary on the internal implementation, and in IE and Firefox (other browsers, of course, no doubt) have good performance. Speed is zero millisecond level.
Achieve 11
01.String.prototype.trim = function () (
02. Var str = this,
03. Str = str.replace (/ ^ \ s + /,'');
04. For (var i = str.length - 1; i> = 0; i -) (
05. If (/ \ S / .test (str.charAt (i))) (
06. Str = str.substring (0, i + 1);
07. Break;
08.)
09.)
10. Return str;
11.)
Achieve 10 have told us that the ordinary native string interception method is far superior to regular replacement, though a bit more complex. But as long as is not too complicated, we can use the browser to the regular optimization, improving the efficiency of program execution, such as the realization of 8 in the performance of IE. I do not usually applied in the project was to achieve 10, because the whitespace too long and too difficult to achieve in mind the (Of course, if you build a library, it is absolutely the first). 11 can be said to achieve its improved version, the front part of the blank is replaced by the positive charge of cutting off, followed by native approach, the result is not less favorable than the original, but the speed is very Guards.
Achieve 12
1.String.prototype.trim = function () (
2. Var str = this,
3. Str = str.replace (/ ^ \ s \ s * /,''),
4. Ws = / \ s /,
5. I = str.length;
6. While (ws.test (str.charAt (- i)));
7. Return str.slice (0, i + 1);
8.)
To achieve 10 and 11 in writing to achieve a better improved version, note that the speed is not the performance, but easy to remember and use. And its two predecessors, are zero-millisecond level, then went to work and use this scary.