In IE 7 8 browsers, if you use the trim () property to remove spaces, it can cause an error.
The solution to this problem is therefore as follows:
var AA = $ ("#id"). Val (). Trim ()---cannot resolve the trim () method in IE
Solution:
[var AA = $.trim ($ ("#id"). Val ()); This is not good to use, or use the following introduction, the first has been tested.
The people in the consortium were kicked in the head 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 common to remove whitespace from both sides of the string, the large class libraries have its shadow. In addition, foreigners are very research energy, drum up quite a lot of realization.
To achieve 1 OK. Write this in JS and follow it directly behind the string you want to go to. Trim ()
Copy Code code as follows:
String.prototype.trim = function () {
return this. Replace (/^\s\s*/, '). Replace (/\s\s*$/, ');
}
It does not look very good, using two of regular replacements, the actual speed is amazing, mainly thanks to the internal optimization of the browser. A well-known example string concatenation, the direct addition is faster than the stringbuffer made of array. This implementation is applied to the Base2 class library.
Achieve 2
Copy Code code as follows:
String.prototype.trim = function () {
return this. Replace (/^\s/, '). Replace (/\s $/, ");
}
Similar to implementation 1, but slightly slower, mainly because it was first assumed that there was at least one blank character. Prototype.js Use this implementation, but its name is strip, because the Prototype method is to try to duplicate Ruby.
Achieve 3
Copy Code code as follows:
String.prototype.trim = function () {
Returnthis. Substring (Math.max (/\s/), 0), this. Search (/\s\s*$/) 1);
}
A total of 4 native methods were called by intercepting the empty part (which, of course, allows for whitespace in the middle). The preset is ingenious, and the substring takes two digital parameters. Math.max with two digital parameters, search is returned to a digital. It's a little slower than the top two, but it's faster than most of the bottom.
Achieve 4
Copy Code code as follows:
String.prototype.trim = function () {
Returnthis. Replace (/^\s |\s $/g, ");
}
This can be called the implementation of the 2 simplified version, is the use of candidate operators to connect two regular. But this has failed the browser optimization opportunity, compared to the implementation of 3. Because it looks elegant, many libraries use it, such as jquery and MooTools
Achieve 5
Copy Code code as follows:
String.prototype.trim = function () {
var str = this;
str = Str.match (/\s (?: \ S \s) * *);
Return str? Str[0]: ';
}
Match is the return of an array, which is the element of the original string as it fits the requirements. To prevent whitespace in the middle of the string from being lifted, we need to draw on the non-capture grouping (?: EXP). Since the array may be empty, we have to make a further decision later. As if the browser in the processing group is weak, a word slow. So don't be superstitious, though it's basically omnipotent.
Achieve 6
Copy Code code as follows:
String.prototype.trim = function () {
return this. Replace (/^\s* (\s* (\s \s) *) \s*$/, ' $ ');
}
Provide the required parts and put them in an empty string. But the efficiency is very poor, especially in the IE6.
Achieve 7
Copy Code code as follows:
String.prototype.trim = function () {
return this. Replace (/^\s* \s* (?: \ S \s) * \s*$/, ' $ ');
}
is similar to implementation 6, but it has the advantage of non capture grouping, with a little improvement in performance.
Achieve 8
Copy Code code as follows:
String.prototype.trim = function () {
return this. Replace (/^\s* (?: [\s\s]*\s)?) \s*$/, ' $ ');
}
Along the top two ideas to improve the use of non-capture grouping and character sets, with the replacement of the *, the effect is amazing. Especially in the IE6, you can use crazy to describe the performance of the promotion, direct seconds to kill Firefox.
Achieve 9
Copy Code code as follows:
String.prototype.trim = function () {
return this. replace (/^\s* ([\s\s]*?) \s*$/, ' $ ');
}
This is a lazy match to replace the capture group, in Firefox improved, ie not the last time so crazy.
Achieve 10
Copy Code code as follows:
String.prototype.trim = function () {
var str = this,
whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u20 05\u2006\u2007\u2008\u2009\u200a\u200b\u2028\ U2029\ u3000 ';
for (var i = 0,len = Str.length i = 0; i--) {
if (Whitespace.indexof (Str.charat (i)) = = 1) {
str = str.substring (0, I 1);
break;
}
}
Return Whitespace.indexof (Str.charat (0)) = = 1? STR: ';
}
I just want to say that the person who made this is not a cow to describe, is already the same level of God. It first lists all the possible whitespace characters, in the first traversal cut off the front blank, the second cut off the back of the blank. The whole process uses only indexof and substring, a native method specially designed to handle strings, without applying regular. Surprisingly fast, it is expected to be on the inside of the binary implementation, and in IE and Firefox (other browsers, of course, there is no doubt) have outstanding performance. The speed is 0 milliseconds, plus.
Achieve 11
Copy Code code as follows:
String.prototype.trim = function () {
var str = this,
str = str.replace (/^\s/, "");
for (var i = str.length-1 i >= 0; i--) {
if (/\s/.test (Str.charat (i))) {
str = str.substring (0, I 1);
break;
}
}
return str;
}
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 a little more complex. But as long as it is not too complicated, we can use the browser for regular optimization, improve program execution efficiency, from the implementation of 8 in IE performance. I don't think anyone would normally apply 10 to a project because that whitespace is too long to remember (of course if you're building a class library, it's definitely the first one). Implementation of the 11 can be described as its improved version, the front part of the blank by the regular replacement is responsible for cutting off, the back with the original method of treatment, the effect is inferior to the original, but the speed is very reverse days.
Achieve 12
Copy Code code as follows:
String.prototype.trim = function () {
var str = this,
str = str.replace (/^\s\s*/, ""),
WS =/\s/,
i = str.length;
while (Ws.test (Str.charat));
Return Str.slice (0, I 1);
}
Implementation of the 10 and implementation of the 11 in the formulation of a better version of the note that is not the performance of speed, but easy to remember and application. And two of its predecessors were 0 milliseconds, and then they used it to work and scare.
The following is a foreigner given the results of the comparison, the implementation of the background is the Magna Carta this article (more than 27,600 characters) for the trim operation.
Implement 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 to realize their own ideas, want to understand what the original author said please see the original.
JS to remove the space method there are currently 12 kinds:
Achieve 1
String.prototype.trim = function () {return this.replace (/^\s\s*/, '). Replace (/\s\s*$/, ');
Achieve 2
String.prototype.trim = function () {return this.replace (/^\s+/, '). Replace (/\s+$/, ');
Achieve 3
String.prototype.trim = function () {return THIS.S string (Math.max (This.search (/\s/), 0), This.search (/\s\s*$/) + 1);}
Achieve 4
String.prototype.trim = function () {return this.replace (/^\s+|\s+$/g, ');}
String.prototype.trim = function () {var str = this; str = Str.match/\s+ (?: \ s+\s+) * *); Return str? Str[0]: '; }
String.prototype.trim = function () {return this.replace (/^\s* \s* (\s+\s+) *) \s*$/, ' $ ';}
Achieve 7
String.prototype.trim = function () {return this.replace/^\s* \s* (?: \ s+\s+) * \s*$/, ' $ '); }
String.prototype.trim = function () {return this.replace/^\s* ((?: [\s\s]*\s)?) \s*$/, ' $ '); }
String.prototype.trim = function () {return this.replace/^\s* ([\s\s]*?) \s*$/, ' $ '); }
String.prototype.trim = function () {var str = this, whitespace = ' \n\r\t\f\x0b\xa0\?\?\?\?\?\?\?\?\?\?\?\?\?\?\ '; var i = 0,len = Str.length; i < Len; i++) {if (Whitespace.indexof (Str.charat (i)) = = 1) {str = STR.S string (i); break;}} for (i = str.length-1; I >= 0; i--) {if (Whitespace.indexof (Str.charat (i)) = = 1) {str = STR.S string (0, i + 1); break;} return Whitespace.indexof ( Str.charat (0)) = = 1? STR: '; }
Achieve 11
String.prototype.trim = function () {var str = this, str = str.replace (/^\s+/, "); for (var i = str.length-1; I >= 0 ; i--) {if (/\s/.test (Str.charat (i))) {str = STR.S string (0, i + 1); break;} return str; }
Achieve 12
String.prototype.trim = function () {var str = this, str = str.replace (/^\s\s*/, "), WS =/\s/, i = str.length; while (ws . Test (Str.charat (i.)); Return Str.slice (0, i + 1); }
It does not look very good, using two of regular replacements, the actual speed is very amazing, mainly thanks to the internal optimization of the browser. A well-known example string concatenation, the direct addition is faster than the stringb?r made of array. The Base2 class library uses this implementation.
Similar to implementation 1, but slightly slower, mainly because it was first assumed that there was at least one blank character. Prototype.js uses this implementation, but its name is strip, because the Prototype method seeks to have the same name as R y.
A total of four native methods were called by intercepting the empty part (which, of course, allows for whitespace in the middle). Cleverly designed, s string takes two digits as an argument. Math.max takes two digits as a parameter and search returns a number. Slower than the top two, but faster than most of the following.
This can be called the implementation of the 2 simplified version, is the use of candidate operators to connect two regular. But doing so loses the chance to optimize the browser, compared to the implementation of 3. Because it appears to be elegant, many class libraries use it, such as Jqry and MooTools
Achieve 5
Match is the return of an array, so the original string conforms to the required part of its element. To prevent whitespace in the middle of the string from being excluded, we need to draw on the non-capture grouping (?: EXP). Since the array may be empty, we have to make further decisions later. As if the browser in the processing group is more powerless, a word slow. So don't be superstitious, though it's basically omnipotent.
Achieve 6
Provide the part that meets the requirements and place it in an empty string. But the efficiency is very poor, especially in the IE6.
is similar to implementation 6, but it has the advantage of non capture grouping, with a little improvement in performance.
Achieve 8
Along the above two ideas for improvement, the use of non-capture grouping and character sets, with the replacement of *, the effect is very amazing. Especially in the IE6, you can use crazy to describe the performance of the promotion, direct seconds to kill Firefox.
Achieve 9
This is a lazy match to replace the capture group, in Firefox improved, ie not the last time so crazy.
Achieve 10
I just want to say that the person who made this is no longer a cow to describe the same level as God. It first lists all the possible whitespace characters, cuts the front blanks in the first traversal, and cuts back the blanks for the second time. The whole process uses only the indexof and S string, a native method that is specifically designed to handle strings, and is not used to regular. Surprisingly fast, it is estimated that the internal binary implementation, and in IE and Firefox (other browsers, of course, there is no doubt) have a good performance. The speed is 0 millisecond level.
Implementation 10 has told us that ordinary native string interception methods are far better than regular replacements, albeit a bit more complex. But as long as it is not too complex, we can use the browser for regular optimization, improve program execution efficiency, such as the implementation of 8 in IE performance. I don't think anyone would normally apply 10 to a project because that whitespace implementation is too long and too hard to remember (of course if you're building a class library, it's definitely the first). Implementation of the 11 can be described as its improved version, the front part of the blank by the regular replacement is responsible for cutting off, the back with the original method of treatment, the effect is not inferior to the original, but the speed is very adverse days.
Implementation of the 10 and implementation of the 11 in the formulation of a better version of the note that is not performance speed, but easy to remember and use. And its two predecessors are 0 millisecond levels, and later use this to work with the scary.