In ie 7 8 browser, if space is removed using the trim () attribute, an error is reported.
Therefore, there are the following solutions to solve this problem:
Currently, there are 12 methods to remove spaces in JS:
Implementation 1
String. prototype. trim = function () {return this. replace (/^ \ s */,''). replace (/\ s * $ /,'');}
Implementation 2
String. prototype. trim = function () {return this. replace (/^ \ s +/, ''). replace (/\ s + $ /,'');}
Implementation 3
String. prototype. trim = function () {return this. s string (Math. max (this. search (/\ S/), 0), this. search (/\ S \ s * $/) + 1 );}
Implementation 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 * $/, '$1 ');}
Implementation 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] *?) \ 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 :'';}
Implementation 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 ;}
Implementation 12
String. prototype. trim = function () {var str = this, str = str. replace (/^ \ s */, ''), ws =/\ s/, I = str. length; while (ws. test (str. charAt (-- I); return str. slice (0, I + 1 );}
It looks pretty bad. I used two regular expressions to replace it. The actual speed is amazing, mainly thanks to the internal optimization of the browser. A famous example of String concatenation, directly adding StringB made of Array? R is still fast. The base2 class library uses this implementation.
It is similar to implementation 1, but a little slower, mainly because it first assumes that at least one blank character exists. Prototype. js uses this implementation, but its name is strip, because the Prototype method strives to be the same as R y.
The blank part is obtained as an intercept (of course, a blank character is allowed in the middle). A total of four native methods are called. S string uses two numbers as parameters. Math. max takes two numbers as parameters, and search returns a number. The speed is a little slower than the two above, but faster than most below.
This can be called a simplified version of 2, which is to use the candidate operator to connect two regular expressions. However, in doing so, the chance of browser optimization is lost, which is inferior to 3. Because it seems elegant, many class libraries use it, such as JQry and mootools.
Implementation 5
Match is an array, so the elements of the original string that meet the requirements become its element. To prevent the blank characters in the string from being excluded, we need to use non-capturing grouping (? : Exp ). Since the array can be empty, we need to further judge later. It seems that the browser is weak in processing the group and the word is slow. So do not be superstitious about regular expressions, although it is basically omnipotent.
Implementation 6
Provide the required parts and put them in an empty string. However, the efficiency is poor, especially in IE6.
Similar to implementation 6, but non-capturing groups are used to improve the performance.
Implementation 8
We improved the above two ideas and used non-capturing grouping and Character Set combination? Replaced by *, the effect is amazing. Especially in IE6, we can use crazy words to describe the performance improvement and kill Firefox in seconds.
Implementation 9
This time, we replaced the non-capturing group with lazy match, which was improved in Firefox. IE was not as crazy as it was last time.
Implementation 10
I just want to say that the person who came up with this is not described by the ox, but is already of the same level as God. It first lists all possible blank characters, removes the leading space in the first traversal, and removes the trailing space in the second traversal. In the whole process, only the native method indexOf and s string are used to process strings, but no regular expressions are used. The speed is amazing. It is estimated that the internal binary implementation is directly pushed, and both IE and Firefox (other browsers certainly have no doubt) have a good performance. The speed is zero milliseconds.
Implementation 10 tells us that the common native string truncation method is far better than regular expression replacement, although it is a little more complicated. However, as long as the regular expression is not complex, we can use the browser to optimize the regular expression to improve the program execution efficiency, such as 8 in IE performance. I don't think anyone will apply 10 in the project, because the whitespace implementation is too long and hard to remember (of course, if you are creating a class library, it is definitely the first one ). 11 is the ultimate version. The blank content in the previous section is cut down by regular expression replacement, and the Native method is used later. The effect is not inferior to that in the original version, but the speed is very backward.
Implementation 10 and implementation 11 are better written in simplified versions. Note that it is not about performance speed, but easy to remember and use. And its two predecessors are both zero-millisecond-level, and they will be used to work and be scary in the future.