在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在写法上更好的改进版,注意说的不是性能速 度,而是易记与使用上。和它的两个前辈都是零毫秒级别的,以后就用这个来工作与吓人。

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
