日期:2014-05-16  浏览次数:20500 次

JS--鍘婚櫎绌烘牸鐨?2绉嶆柟娉?鍏煎鍚勭娴忚鍣?
JS鍘婚櫎绌烘牸鐨勬柟娉曠洰鍓嶅叡鏈?2绉?

瀹炵幇1

String.prototype.trim = function() {    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');  }鐪嬭捣鏉ヤ笉鎬庝箞鏍凤紝 鍔ㄧ敤浜嗕袱娆℃鍒欐浛鎹紝瀹為檯閫熷害闈炲父鎯婁汉锛屼富瑕佸緱鐩婁簬娴忚鍣ㄧ殑鍐呴儴浼樺寲銆備竴涓憲鍚嶇殑渚嬪瓙瀛楃涓叉嫾鎺ワ紝鐩存帴鐩稿姞姣旂敤Array鍋氭垚鐨凷tringB?r 杩樺揩銆俠ase2绫诲簱浣跨敤杩欑瀹炵幇銆?
瀹炵幇2

String.prototype.trim = function() {    return this.replace(/^\s+/, '').replace(/\s+$/, '');  }鍜屽疄鐜? 寰堢浉浼硷紝浣嗙◢鎱竴鐐癸紝涓昏鍘熷洜鏄畠鏈€鍏堟槸鍋囪鑷冲皯瀛樺湪涓€涓┖鐧界銆侾rototype.js浣跨敤杩欑瀹炵幇锛屼笉杩囧叾鍚嶅瓧涓簊trip锛屽洜涓?Prototype鐨勬柟娉曢兘鏄姏姹備笌R y鍚屽悕銆?
瀹炵幇3

String.prototype.trim = function() {    return  this.s string(Math.max(this.search(/\S/), 0),this.search(/\S\s*$/) + 1);  }浠ユ埅鍙栨柟寮忓彇寰楃┖鐧介儴鍒嗭紙褰撶劧鍏佽涓棿瀛樺湪绌虹櫧绗︼級锛屾€诲叡 璋冪敤浜嗗洓涓師鐢熸柟娉曘€傝璁″緱闈炲父宸у锛宻 string浠ヤ袱涓暟瀛椾綔涓哄弬鏁般€侻ath.max浠ヤ袱涓暟瀛椾綔鍙傛暟锛宻earch鍒欒繑鍥炰竴涓暟瀛椼€傞€熷害姣斾笂 闈袱涓參涓€鐐癸紝浣嗘瘮涓嬮潰澶у鏁伴兘蹇€?
瀹炵幇4

String.prototype.trim = function() {    return  this.replace(/^\s+|\s+$/g, '');  }杩欎釜鍙互绉板緱涓婂疄鐜?鐨勭畝鍖栫増锛屽氨鏄?鍒╃敤鍊欓€夋搷浣滅杩炴帴涓や釜姝e垯銆備絾杩欐牱鍋氬氨澶卞幓浜嗘祻瑙堝櫒浼樺寲鐨勬満浼氾紝姣斾笉涓婂疄鐜?銆傜敱浜庣湅鏉ュ緢浼橀泤锛岃澶氱被搴撻兘浣跨敤瀹冿紝濡侸Qry涓巑ootools

瀹炵幇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');  }鍜屽疄鐜?寰堢浉浼硷紝浣嗙敤浜嗛潪鎹曡幏鍒嗙粍杩涜浜嗕紭鐐癸紝鎬ц兘鏁堜箣鏈変竴鐐圭偣鎻愬崌銆?


瀹炵幇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');  }杩欐鏄敤鎳掓儼鍖归厤 椤舵浛闈炴崟鑾峰垎缁勶紝鍦ㄧ伀鐙愪腑寰楀埌鏀瑰杽锛孖E娌℃湁涓婃閭d箞鐤媯銆?


瀹炵幇10

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 : '';  }  鎴?鍙兂璇达紝鎼炲嚭杩欎釜鐨勪汉宸茬粡涓嶆槸鐢ㄧ墰鏉ュ舰瀹癸紝宸叉槸绁炰竴鏍风殑绾у埆銆傚畠鍏堟槸鎶婂彲鑳界殑绌虹櫧绗﹀叏閮ㄥ垪鍑烘潵锛屽湪绗竴娆¢亶鍘嗕腑鐮嶆帀鍓嶉潰鐨勭┖鐧斤紝绗簩娆$爫鎺夊悗闈㈢殑绌虹櫧銆傚叏 杩囩▼鍙敤浜唅ndexOf涓巗 string杩欎釜涓撻棬涓哄鐞嗗瓧绗︿覆鑰岀敓鐨勫師鐢熸柟娉曪紝娌℃湁浣跨敤鍒版鍒欍€傞€熷害蹇緱鎯婁汉锛屼及璁$洿閫间笂鍐呴儴鐨勪簩杩涘埗瀹炵幇锛屽苟涓斿湪 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.s string(0, i + 1);        break;      }    }    return str;  }瀹炵幇10宸茬粡鍛婅瘔鎴戜滑鏅€氱殑鍘熺敓瀛楃涓叉埅鍙栨柟娉曟槸杩滆儨浜庢鍒欐浛鎹紝铏界劧鏄鏉備竴鐐广€備絾鍙姝e垯 涓嶈繃浜庡鏉傦紝鎴戜滑灏卞彲浠ュ埄鐢ㄦ祻瑙堝櫒瀵规鍒欑殑浼樺寲锛屾敼鍠勭▼搴忔墽琛屾晥鐜囷紝濡傚疄鐜?鍦↖E鐨勮〃鐜般€傛垜鎯抽€氬父涓嶄細鏈変汉鍦ㄩ」鐩腑搴旂敤瀹炵幇10锛屽洜涓洪偅涓?whitespace 瀹炵幇澶暱澶毦璁颁簡锛堝綋鐒跺鏋滀綘鍦ㄦ墦閫犱竴涓被搴擄紝瀹冪粷瀵规槸棣栧厛锛夈€傚疄鐜?1鍙皳鍏舵敼杩涚増锛屽墠闈㈤儴鍒嗙殑绌虹櫧鐢辨鍒欐浛鎹㈣礋璐g爫鎺夛紝鍚庨潰鐢ㄥ師鐢熸柟娉曞鐞嗭紝鏁堟灉涓嶉€?浜庡師鐗堬紝浣嗛€熷害閮芥槸闈炲父閫嗗ぉ銆?
瀹炵幇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涓庡疄鐜?1鍦ㄥ啓娉曚笂鏇村ソ鐨勬敼杩涚増锛屾敞鎰忚鐨勪笉鏄€ц兘閫?搴︼紝鑰屾槸鏄撹涓庝娇鐢ㄤ笂銆傚拰瀹冪殑涓や釜鍓嶈緢閮芥槸闆舵绉掔骇鍒殑锛屼互鍚庡氨鐢ㄨ繖涓潵宸ヤ綔涓庡悡浜恒€?