Home Web Front-end JS Tutorial ie 7/8不支持trim的属性的解决方案_javascript技巧

ie 7/8不支持trim的属性的解决方案_javascript技巧

May 16, 2016 pm 04:47 PM
trim

在ie 7 8浏览器中,如果使用trim()属性去除空格的话,则会导致报错。

因此解决这个问题有如下方案:

var aa = $("#id").val().trim()   --- 在IE中无法解析trim() 方法

解决办法:

[   var aa = $.trim($("#id").val());  ] 这个不好用,还是用下面介绍的吧,第一个已经过测试。
 

W3C那帮人的脑袋被驴踢了,直到java script1.8.1才支持trim函数(与trimLeft,trimRight),可惜现在只有 firefox3.5支持。由于去除字符串两边的空白实在太常用,各大类库都有它的影子。加之,外国人都很有研究精力,搞鼓了相当多实现。

实现1  OK  的。(在js中写上这个,然后直接在你要去空格的字符串后面跟上 .trim() 即可)

复制代码 代码如下:

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

看起来不怎么样,动用了两次正则替换,实际速度很是惊人,主要得益于浏览器的内部优化。一个著名的例子字符串拼接,直接相加比用Array做成的StringBuffer还快。base2类库施用这种实现。

实现2

复制代码 代码如下:

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

和实现1很相似,但稍慢一点,主要原因是它最先是假设至少存在一个空白符。Prototype.js施用这种实现,不过其名儿为strip,因为Prototype的方法都是力图与Ruby重名。

实现3

复制代码 代码如下:

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

以截取方式取得空白部分(当然允许中间存在空白符),总共调用了4个原生方法。预设得很是巧妙,substring以两个数码作为参数。Math.max以两个数码作参数,search则归回一个数码。速度比上边两个慢一点,但比下面大大都都快。

实现4

复制代码 代码如下:

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

这个可以称得上实现2的简化版,就是利用候选操作符连接两个正则。但这样做就落空了浏览器优化的机会,比不上实现3。由于看来很优雅,许多类库都施用它,如JQuery与mootools

实现5

复制代码 代码如下:

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

match是归回一个数组,是以原字符串切合要求的部分就成为它的元素。为了防止字符串中间的空白符被解除,咱们需要动用到非捕获性分组(?:exp)。由于数组可能为空,咱们在后面还要做进一步的判定。好像浏览器在处理分组上比力无力,一个字慢。所以不要迷信正则,虽然它基本上是万能的。

实现6

复制代码 代码如下:

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

把切合要求的部分提供出来,放到一个空字符串中。不过效率很差,尤其是在IE6中。

实现7

复制代码 代码如下:

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

和实现6很相似,但用了非捕获分组进行了优点,性能效之有一点点提升。

实现8

复制代码 代码如下:

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

沿着上边两个的思路进行改进,动用了非捕获分组与字符集合,用?顶替了*,效果很是惊人。尤其在IE6中,可以用疯狂来形容这次性能的提升,直接秒杀火狐。

实现9

复制代码 代码如下:

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

这次是用懒惰匹配顶替非捕获分组,在火狐中得到改善,IE没有上次那么疯狂。

实现10

复制代码 代码如下:

 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 : '' ;
 }

我只想说,搞出这个的人已不是用牛来形容,已是神一样的级别。它先是把可能的空白符全部列出来,在第一次遍历中砍掉前边的空白,第二次砍掉后面的空白。全过程只用了indexOf与substring这个专门为处理字符串而生的原生方法,没有施用到正则。速度快得惊人,预计直逼上内部的二进制实现,并且在IE与火狐(其它浏览器当然也毫无疑问)都有杰出的表现。速度都是零毫秒级另外。

实现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;
 }

实现10已告诉咱们普通的原不认识的字符串截取方法是远胜于正则替换,虽然是复杂一点。但只要正则不过于复杂,咱们就可以利用浏览器对正则的优化,改善程序执行效率,从实现8在IE的表现。我想通常不会有人在项目中应用实现10,因为那个whitespace 实现过长太难记了(当然如果你在打造一个类库,它绝对是起首)。实现11可谓其改进版,前边部分的空白由正则替换负责砍掉,后面用原生方法处理,效果不逊于原版,但速度都是很是逆天。

实现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);
 }

实现10与实现11在写法上更好的改进版,注意说的不是性能速度,而是易记与施用上。和它的两个先辈都是零毫秒级另外,以后就用这个来工作与吓人。

下面是老外给出的比力结果,执行背景是对Magna Carta 这文章(超过27,600字符)进行trim操作。

实现 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函数实现揭晓自己的想法,想懂得原作者说什么请看原文。



JS去除空格的方法目前共有12种:

实现1
String.prototype.trim = function() { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }
实现2
String.prototype.trim = function() { return this.replace(/^\s+/, '').replace(/\s+$/, ''); }
实现3
String.prototype.trim = function() { return this.s string(Math.max(this.search(/\S/), 0),this.search(/\S\s*$/) + 1); }
实现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'); }
实现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 = 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 : ''; }
实现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; }
实现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); }

看起来不怎么样, 动用了两次正则替换,实际速度非常惊人,主要得益于浏览器的内部优化。一个著名的例子字符串拼接,直接相加比用Array做成的StringB?r 还快。base2类库使用这种实现。

和实现1 很相似,但稍慢一点,主要原因是它最先是假设至少存在一个空白符。Prototype.js使用这种实现,不过其名字为strip,因为 Prototype的方法都是力求与R y同名。

以截取方式取得空白部分(当然允许中间存在空白符),总共 调用了四个原生方法。设计得非常巧妙,s string以两个数字作为参数。Math.max以两个数字作参数,search则返回一个数字。速度比上 面两个慢一点,但比下面大多数都快。

这个可以称得上实现2的简化版,就是 利用候选操作符连接两个正则。但这样做就失去了浏览器优化的机会,比不上实现3。由于看来很优雅,许多类库都使用它,如JQry与mootools

实现5

match 是返回一个数组,因此原字符串符合要求的部分就成为它的元素。为了防止字符串中间的空白符被排除,我们需要动用到非捕获性分组(?:exp)。由于数组可 能为空,我们在后面还要做进一步的判定。好像浏览器在处理分组上比较无力,一个字慢。所以不要迷信正则,虽然它基本上是万能的。

实现6

把符合要求的部分提供出来,放到一个空字符串中。不过效率很差,尤其是在IE6中。

和实现6很相似,但用了非捕获分组进行了优点,性能效之有一点点提升。

实现8

沿着上面两个的思路进行改进,动用了非捕获分组与字符集合,用?顶替了*,效果非常惊人。尤其在IE6中,可 以用疯狂来形容这次性能的提升,直接秒杀火狐。

实现9

这次是用懒惰匹配 顶替非捕获分组,在火狐中得到改善,IE没有上次那么疯狂。

实现10

我 只想说,搞出这个的人已经不是用牛来形容,已是神一样的级别。它先是把可能的空白符全部列出来,在第一次遍历中砍掉前面的空白,第二次砍掉后面的空白。全 过程只用了indexOf与s string这个专门为处理字符串而生的原生方法,没有使用到正则。速度快得惊人,估计直逼上内部的二进制实现,并且在 IE与火狐(其他浏览器当然也毫无疑问)都有良好的表现。速度都是零毫秒级别的。

实现10已经告诉我们普通的原生字符串截取方法是远胜于正则替换,虽然是复杂一点。但只要正则 不过于复杂,我们就可以利用浏览器对正则的优化,改善程序执行效率,如实现8在IE的表现。我想通常不会有人在项目中应用实现10,因为那个 whitespace 实现太长太难记了(当然如果你在打造一个类库,它绝对是首先)。实现11可谓其改进版,前面部分的空白由正则替换负责砍掉,后面用原生方法处理,效果不逊 于原版,但速度都是非常逆天。

实现10与实现11在写法上更好的改进版,注意说的不是性能速 度,而是易记与使用上。和它的两个前辈都是零毫秒级别的,以后就用这个来工作与吓人。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Practical Tips: How to use the trim function in PHP to process Chinese spaces Practical Tips: How to use the trim function in PHP to process Chinese spaces Mar 27, 2024 am 11:27 AM

In PHP programming, spaces are often encountered when processing strings, including Chinese spaces. In actual development, we often use the trim function to remove spaces at both ends of a string, but the processing of Chinese spaces is relatively complicated. This article will introduce how to use the trim function in PHP to process Chinese spaces and provide specific code examples. First, let us understand the types of Chinese spaces. In Chinese, spaces include not only common English spaces (space), but also some other special spaces.

Guide to using trim() function in PHP Guide to using trim() function in PHP Feb 20, 2024 am 08:39 AM

Guide to using the trim() function in PHP The trim() function is very commonly used in PHP and is used to remove spaces or other specified characters at the beginning and end of a string. This article will introduce the use of trim() function in detail and provide specific code examples. 1. Function syntax The syntax of the trim() function is as follows: trim(string$str,string$character_mask=""):string This function accepts two parameters,

Use the strings.Trim function to remove the specified character set from the beginning and end of a string Use the strings.Trim function to remove the specified character set from the beginning and end of a string Jul 24, 2023 pm 04:27 PM

Use the strings.Trim function to remove the specified character set at the beginning and end of a string. In the Go language, the strings.Trim function is a very practical function that can remove the specified character set at the beginning and end of a string, making the string more tidy and standardized. This article will introduce how to use the strings.Trim function and show some code examples. First, let’s take a look at the prototype of the strings.Trim function: funcTrim(sstring,cutsetstri

What is the function of trim function of solid state drive? What is the function of trim function of solid state drive? Nov 21, 2022 am 10:58 AM

The trim function of the solid-state drive is mainly to optimize the solid-state drive, solve the problem of slowdown and lifespan of the SSD after use, and improve the efficiency of the SSD by preparing data blocks for reuse. The Trim function is a function that almost all SSD solid state drives have. It is an ATA command. When the system confirms that the SSD supports Trim and deletes data, it does not notify the hard disk of the deletion command and only uses the Volume Bitmap to remember that the data here has been deleted. This enables faster data processing.

Use the PHP function 'trim' to remove whitespace characters at both ends of a string Use the PHP function 'trim' to remove whitespace characters at both ends of a string Jul 25, 2023 pm 04:45 PM

The PHP function "trim" is a very useful string processing function. It can help us remove whitespace characters at both ends of the string, including spaces, tabs, newlines, etc. When writing PHP programs, we often encounter situations where user input needs to be cleaned. At this time, using "trim" can ensure that we get a clean string and avoid errors caused by irregular user input. Here is a simple code example showing how to use the "trim" function: <?php

How to enable and disable TRIM on Windows 11 How to enable and disable TRIM on Windows 11 Sep 29, 2023 pm 03:13 PM

Using an SSD drive leaves you with the constant worry of losing your data and being unable to recover it. However, Windows allows you to achieve optimal performance by executing TRIM commands that write only necessary data without having to manage old data blocks. To do this, you need to make sure your SSD supports TRIM and enable it in your operating system. How to check if TRIM is enabled? In most cases, TRIM functionality is enabled by default in modern SSDs. But to make sure this is checked out, you can run the command with administrative rights. Just open an elevated command prompt, run the fsutil behavioral query DisableDeleteNotify command and your SSD will be listed. 0 means enabled, 1 means disabled. like

PHP tutorial: Learn to use the trim function to remove Chinese spaces PHP tutorial: Learn to use the trim function to remove Chinese spaces Mar 27, 2024 am 11:00 AM

PHP Tutorial: Learn to use the trim function to remove Chinese spaces, you need specific code examples. In PHP development, you often encounter situations where you need to process strings, and one of the common requirements is to remove spaces at both ends of the string. When processing English strings, we can directly use PHP's built-in trim function to achieve this operation. However, when the string contains Chinese characters, the trim function may not be able to remove Chinese spaces normally. In this case, some special processing is required. method to achieve our needs. This article will introduce

How to use trim in Java How to use trim in Java May 02, 2023 pm 01:31 PM

1. Explain that trim() is the most commonly used method by Java developers to remove leading and trailing spaces. For the trim() method, a space character is any character with an ASCII value less than or equal to 32('U+0020')*. The trim() method returns a copy of the calling string object, but with all beginnings and ends removed. 2. Instance publicclassFunTester{publicstaticvoidmain(String[]args){Stringstring="onetwothree";System.out.prin

See all articles