Ie 7 and 8 does not support trim's attribute solution

  • 2020-03-30 03:03:10
  • OfStack

In ie 7 and 8 browsers, removing whitespaces with the trim() attribute results in an error.

Therefore, there are the following solutions to this problem:

Var aa = $(" # id "). Val (). The trim ()     -- the trim() method cannot be resolved in IE

Solutions:

Var aa = $.trim($("#id").val()); ]
 

To achieve 1   OK  . (write this in js and follow.trim() directly after the string you want to whitespace.)


 String.prototype.trim = function () {
return this .replace(/^ss*/, '' ).replace(/ss*$/, '' );
 }

It didn't look good, it took two regular substitutions, and the actual speed was amazing, thanks largely to the browser's internal optimizations. A famous example of string concatenation is the direct addition of strings faster than the StringBuffer made with Array. The base2 class library applies this implementation.

The 2


 String.prototype.trim = function () {
return this .replace(/^s /, '' ).replace(/s $/, '' );
 }

It's similar to implementation 1, but a bit slower, mainly because it starts with the assumption that at least one blank character exists. Prototype.js USES this implementation, but it's called strip, because Prototype's methods try to be named after Ruby.

Implement 3


 String.prototype.trim = function () {
returnthis .substring(Math.max( this .search(/S/), 0), this .search(/Ss*$/) 1);
 }

In total, four native methods were called by intercepting the white space (with white space allowed, of course). By default,substring takes two digits. Math.max takes two digits, and search returns one. It's a little slower than the top two, but much faster than the bottom two.

To achieve 4


 String.prototype.trim = function () {
returnthis .replace(/^s |s $/g, '' );
 }

This is a simplified version of implementation 2, which USES candidate operators to join two regularisms. But doing so misses the opportunity for browser optimization, not implementation 3. Because it looks elegant, many libraries use it, such as JQuery and mootools

Implement 5


 String.prototype.trim = function () {
var str = this ;
str = str.match(/S (?:s S )*/);
return str ? str[0] : '' ;
 }

Match returns an array whose elements are the appropriate parts of the original string. To prevent the white space in the middle of the string from being unwound, we need to use the non-capture group (? : exp). Since the array may be empty, we will make further decisions later. It seems that the browser is much slower than it is at grouping, one word at a time. So don't be superstitious about regularity, which is basically omnipotent.

To achieve 6


 String.prototype.trim = function () {
return this .replace(/^s*(S*(s S )*)s*$/, '$1' );
 }
 

Provide the appropriate part into an empty string. However, it is very inefficient, especially in IE6.

Realization of 7


 String.prototype.trim = function () {
return this .replace(/^s*(S*(?:s S )*)s*$/, '$1' );
 }
 

Similar to implementation 6, but with the benefit of non-capture grouping, performance is slightly improved.

To achieve 8


 String.prototype.trim = function () {
return this .replace(/^s*((?:[Ss]*S)?)s*$/, '$1' );
 }
 

Along the above two ideas to improve the use of non - capture grouping and character set, with? Replace the *, the effect is amazing. Especially in IE6, you can use crazy to describe this performance improvement, directly kill firefox.

Implement 9


 String.prototype.trim = function () {
return this .replace(/^s*([Ss]*?)s*$/, '$1' );
 }
 

Instead of a non-capture group, lazy matching is used this time, which is improved in firefox,IE is not as crazy as last time.

Achieve 10


 String.prototype.trim = function () {
var str = this ,
whitespace = ' nrtfx0bxa0u2000u2001u2002u2003u2004u20 05u2006u2007u2008u2009u200au200bu2028u2029 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 created this is no longer described as a cow, but as a god. It starts with a list of possible whitespace characters, cuts the leading whitespace on the first pass, and the trailing whitespace on the second pass. The whole process USES indexOf and substring, which are native methods for dealing with strings, rather than regularization. It is surprisingly fast, is expected to go straight to the internal binary implementation, and has done well in both IE and firefox (and no doubt in other browsers). The speed is zero milliseconds plus.

Realization of 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.substring(0, i 1);
break ;
}
}
return str;
 }

Implementation 10 has taught us that the common, unrecognized method of string interception is far superior to regular substitution, although it is a bit more complex. But as long as the regularization is not too complex, we can take advantage of the browser to regularization optimization, improve the execution efficiency of the program, from the realization of 8 in IE performance. I don't think anyone usually applies implementation 10 in a project because the whitespace implementation is too long to remember (of course if you're building a library, it's definitely the first). Implementation 11 is an improved version, the front part of the white space is responsible for the regular replacement to cut, followed by the native method, the effect is not inferior to the original, but the speed is very bad.

Implementation of 12


 String.prototype.trim = function () {
var str = this ,
str = str.replace(/^ss*/, '' ),
ws = /s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i 1);
 }

Implementation 10 and implementation 11 are better improvements in notation, not in terms of performance speed, but in terms of ease of use. And both of its predecessors were in the zero millisecond scale and so on.

Below is the comparison result given by the foreigner, in the context of the Magna Carta article (over 27,600 characters) trim operation.

Implement Firefox 2, IE 6

Trim1 15ms trim2 31ms trim3 46ms 31ms
Trim4 47 ms and ms
Trim5 156 ms to 1656 ms
Trim6 172 ms to 2406 ms
Trim7 172 ms to 1640 ms
Trim8 281ms trim9 125ms 78ms

Trim10 trim11 trim12 trim function implementation reveal their ideas, want to understand what the original author said see the text.



There are currently 12 methods of JS to remove Spaces:

To achieve 1
String. The prototype. The trim = function () {return this. The replace (/ ^ \ \ s * / s, ' '). The replace (/ \ \ s * s $/, "); }
The 2
String. The prototype. The trim = function () {return this. The replace (/ ^ \ s + / "). The replace (/ \ s + $/, "); }
Implement 3
String.prototype.trim = function() {return this.s String (math.max (this.search(/\ s /), 0),this.search(/\ s \s*$/) + 1); }
To 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 the STR? STR [0] : "'; }
String. The prototype. The trim = function () {return this. The replace (/ ^ \ s * (\ s * (\ s + \ s +) *) \ s * $/, "$1"); }
Realization of 7
String.prototype.trim = function() {return this.replace(/^\s*(\ s*(? : \ \ s + s +) *) \ s * $/, "$1"); }
String.prototype.trim = function() {return this.replace(/^\s*) : [\ S \ S] * \ S?) \ s * $/, "$1"); }
String.prototype.trim = function() {return this.replace(/^\s*([\ s]*?)) \ s * $/, "$1"); }
String.prototype.trim = function() {var STR = this, whitespace = '\n\r\t\f\x0b\xa0\? \? \? \? \? \? \? \? \? \? \? \? \? \? \ '; For (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: "'; }
Realization of 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))) {string STR = STR. S (0, I + 1); Break; }} return STR; }
Implementation of 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 didn't look that good, it took two regex substitutions, and the actual speed was amazing, thanks largely to the browser's internal optimizations. A famous example of string concatenation is the direct sum over the StringB made from Array, right? R. The base2 class library USES this implementation.

It's similar to implementation 1, but a bit slower, mainly because it starts with the assumption that at least one blank character exists. Prototype.js USES this implementation, but it's called strip, because Prototype's methods strive to have the same name as R y.

In total, four native methods are called by intercepting to get the white space (with white space in between, of course). Cleverly designed, s string takes two Numbers as arguments. Math.max takes two Numbers as an argument, and search returns a number. It's a little slower than the top two, but faster than most of the bottom two.

This is a simplified version of implementation 2, which USES candidate operators to join two regularisms. But doing so misses the opportunity for browser optimization, as opposed to implementation 3. Because it looks elegant, many libraries use it, such as JQry with mootools

Implement 5

A match returns an array, so the part of the original string that meets the requirements becomes its element. To prevent the whitespaces in the middle of the string from being excluded, we need to invoke the non-capture grouping (? : exp). Since the array can be null, we will make further decisions later. It seems that the browser is weak on grouping, one word slow. So don't be superstitious about regularity, which is basically omnipotent.

To achieve 6

Provide the required part into an empty string. However, it is very inefficient, especially in IE6.

Similar to implementation 6, but with the benefit of non-capture grouping, performance is slightly improved.

To achieve 8

Along the above two ideas to improve the use of non-capture grouping and character set, with? Replace the *, and the results are amazing. Especially in IE6, can describe this performance improvement with the use of crazy, direct seconds kill firefox.

Implement 9

Instead of a non-capture group, lazy matching is used this time, which is improved in firefox, IE is not as crazy as last time.

Achieve 10

I just want to say that the person who created this is no longer described as a cow, but as a god. It starts with a list of all possible whitespace characters, cutting off the first whitespace and the second whitespace. The whole process only USES indexOf and s string, which are native methods for dealing with strings, and does not use regularization. It is surprisingly fast, probably up to the internal binary implementation, and works well in IE and firefox (and no doubt in other browsers). The speed is zero milliseconds.

Implementation 10 has shown us that the normal native string interception method is much better than regular substitution, although it is a bit more complicated. But as long as the regularization is not too complex, we can use the browser to regularization optimization, improve the execution efficiency of the program, such as 8 in IE performance. I don't think anyone usually applies implementation 10 in a project because the whitespace implementation is too long to remember (of course if you're building a library, it's definitely the first). Implementation 11 is an improved version, with the first part of the white space to be cut by the regular replacement, and the second part to be processed by the native method, the effect is not as good as the original, but the speed is very bad.

Implementation 10 and implementation 11 are better improvements in writing, not in terms of performance speed, but in terms of ease of use. And both of its predecessors are zero milliseconds, which will be used to work and scare people later.

Related articles: