Http://hi.baidu.com/yuiezt/item/756d0f4ec4d2640ec11613f9
var AA = $ ("#id"). Val (). Trim ()---the trim () method cannot be resolved in IE
Workaround:
[var AA = $.trim ($ ("#id"). Val ()); This is not a good use, or the following is introduced, the first has been tested.
The people's brains were kicked by the donkey until Java script1.8.1 supported the Trim function (and trimleft,trimright), but now only firefox3.5 support. Because it is too often used to remove whitespace on both sides of a string, each large class library has its shadow. In addition, foreigners are very research energy, make a lot of drums to achieve.
Implementation of 1 OK. (write this in JS and then follow the string that you want to go to the space. Trim () to be followed)
1. String.prototype.trim = function () {
2.return this. replace (/^\s\s*/, "). Replace (/\s\s*$/, ');
3.}
It doesn't look very good, with two regular replacements, the actual speed is amazing, thanks to the browser's internal optimization. A well-known example string concatenation, the direct addition than the array made of StringBuffer is also faster. This implementation is applied to the Base2 class library.
Implementation 2
1. String.prototype.trim = function () {
2.return this. replace (/^\s/, "). Replace (/\s $/,");
3.}
Similar to implementation 1, but slightly slower, the main reason is that it first assumes that there is at least one whitespace character. Prototype.js applied this implementation, but its name is strip, because Prototype's approach is to try to duplicate Ruby.
Implementation 3
1. String.prototype.trim = function () {
2.returnthis. Substring (Math.max (this. Search (/\s/), 0), this. Search (/\s\s*$/) 1);
3.}
A total of 4 native methods were called by capturing a blank part (which, of course, allowed the presence of whitespace in the middle). The presets are ingenious, substring with two of digital parameters. Math.max with two digital parameters, search is returned to a digital. Slower than the top two, but much faster than most of the following.
Implementation 4
1. String.prototype.trim = function () {
2.returnthis. Replace (/^\s |\s $/g, ");
3.}
This can be called a simplified version of the implementation of 2, is to use the candidate operator to connect two regular. But doing so misses the chance of browser optimization, which is 3 less than implementation. Because it seems elegant, many libraries apply it, such as jquery and MooTools
Implementation 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 the return of an array, which is the element that fits the requirements of the original string. To prevent the whitespace in the middle of the string from being lifted, we need to use a non-capturing grouping (?: EXP). Since the array may be empty, we need to make a further decision later. As if the browser in the processing group than the weak force, a word slow. So don't be superstitious, though it's basically omnipotent.
Implementation 6
1. String.prototype.trim = function () {
2.return this. Replace (/^\s* (\s* (\s \s) *) \s*$/, ' $ ');
3.}
Provide the part that meets the requirements and put it in an empty string. But the efficiency is very poor, especially in the IE6.
Implementation 7
1. String.prototype.trim = function () {
2.return this. replace (/^\s* (?: \ \ \s* S \s) *) \s*$/, ' $ ');
3.}
Similar to implementation 6, but with the advantage of non-capturing grouping, the performance effect is a little bit improved.
Implementation 8
1. String.prototype.trim = function () {
2.return this. replace (/^\s* (?: [\s\s]*\s)?) \s*$/, ' $ ');
3.}
Along the top two ideas to improve the use of non-capturing groups and character sets, with the replacement of *, the effect is very amazing. Especially in the IE6, you can use madness to describe the performance of the promotion, direct seconds to kill Firefox.
Implementation 9
1. String.prototype.trim = function () {
2.return this. replace (/^\s* ([\s\s]*?) \s*$/, ' $ ');
3.}
This is a lazy match to replace the non-capturing group, in Firefox improved, ie not last so crazy.
Implementation 10
String.prototype.trim = function () {
02.var str = this,
03.whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u20 05\u2006\u2007\u2008\u2009\u200a\u200b\u2028\ U2029\ u3000 ';
04.for (var i = 0,len = str.length; 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 the person who made this is not an ox, it is the same level as God. It first lists the possible whitespace characters, cuts out the front blanks in the first traversal, and the second one cuts back the blanks. The whole process only uses indexof and substring, a native method specifically designed to handle strings, without applying to regular. The speed is staggering, it is expected to be in the internal binary implementation, and in IE and Firefox (other browsers of course, there is no doubt) have outstanding performance. The speed is 0 milliseconds in addition.
Implementation 11
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.}
Implementation 10 has told us that the ordinary original does not know the string interception method is far better than the regular replacement, although it is more complex. But as long as the regular is not too complicated, we can use the browser to optimize the regular, improve program execution efficiency, from the implementation of 8 in IE performance. I don't think anyone will normally apply implementation 10 to a project because that whitespace is too long to remember (if you're building a class library, of course, it's definitely the first one). Implementation 11 is its improved version, the front part of the blank by the regular replacement is responsible for cutting off, followed by the original method of processing, the effect is not inferior to the original, but the speed is very inverse day.
Implementation 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.}
Implementation of 10 and implementation of 11 in a better version of the wording, note that the performance is not speed, but easy to remember and application. And its two ancestors were 0 milliseconds in addition, later on with this to work and scary.
The following is a comparison of the results given by the foreigner, the execution background is to Magna Carta this article (more than 27,600 characters) to trim operations.
Implementing Firefox 2 IE 6
TRIM1 15ms trim2 31ms trim3 46ms 31ms
TRIM4 47ms 46ms
TRIM5 156ms 1656ms
TRIM6 172ms 2406ms
TRIM7 172ms 1640ms
TRIM8 281ms trim9 125ms 78ms
TRIM10 Trim11 trim12 trim function realize their ideas, and want to know what the original author said, please read the original.
Turn: IE cannot use JS trim () workaround