JavaScript에서 문자열을 자르려면 어떻게 하나요?
이후 모든 브라우저에서 IE9+ %s/dbase/ext_table. 써줬지 방법 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim) ['트리밍할 ()']
누가 그 지원하지 않는 브라우저를 트리밍할 () ',' 이 폴리필 매든 에서 사용할 수 있습니다.
if (!String.prototype.trim) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
String.prototype.trim = function() {
return this.replace(rtrim, '');
};
})();
}
즉, jQuery 를 사용하는 경우 ' (str)' 도 ',' $ 스트림 사용할 수 있으며 정의되지 않은 / nulll 처리합니다.
이 슬라이드에서는:
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
String.prototype.ltrim=function(){return this.replace(/^\s+/,'');};
String.prototype.rtrim=function(){return this.replace(/\s+$/,'');};
String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};
비록 여러 가지 위에 있는 'String' 의 JavaScript 객체 정답을 점에 유의해야 합니다 () '는 네이티브' 스트림 vmware. 5 방법을 소개한다. 이 때문에 이상적인 프로토타입 (prototype) 이 이미 여부를 확인할 방법은 어떠한 시도도 트리밍할 받아야 할 것은 처음이다.
if(!String.prototype.trim){
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g,'');
};
}
따라서 에서:
Firefox: 3.5+
사파리: 5+
Internet Explorer: IE9+ (의 표준 모드에만!) http://blogs.msdn.com/b/ie/archive/2010/06/25/enhanced-scripting-in-ie9-ecmascript-5-support-and-more.aspx
크롬. 5+
오페라: 10.5+
ECMAScript 5 지원 Table: http://kangax.github.com/es5-compat-table/
Jquery 를 사용하는 경우 '제크리스트림 ()' 기능을 사용합니다. 예를 들면 다음과 같습니다.
if( jQuery.trim(StringVariable) == '')
http://blog.stevenlevithan.com/archives/faster-trim-javascript
기존의 루프지 있다 비 놀라 울 정규 표현식 기반 느리다.
/**
* Trim string. Actually trims all control characters.
* Ignores fancy Unicode spaces. Forces to string.
*/
function trim(str) {
str = str.toString();
var begin = 0;
var end = str.length - 1;
while (begin <= end && str.charCodeAt(begin) < 33) { ++begin; }
while (end > begin && str.charCodeAt(end) < 33) { --end; }
return str.substr(begin, end - begin + 1);
}
넷윈을 사용하여 JavaScript 방법: ['스트라이스트림레프트 ()'] (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/TrimLeft), '스트라이스트리 라이트 ()' 및 '스트라이스트림 ()'.
< br>; 스트라이스트림 () '가' 에서 IE9+ 및 기타 모든 주요 브라우저:
' Hello '.trim() //-> 'Hello'
< br>; '스트라이스트림레프트 ()' 와 '스트라이스트리 라이트 ()' 이 아니라 [IE 를 제외한 모든 주요 브라우저 ] 에서 지원되지 비표준 4
' Hello '.trimLeft() //-> 'Hello '
' Hello '.trimRight() //-> ' Hello'
< br>; IE 를 통해 손쉽게 이용할 수 있는 폴리필 그러나:
if (!''.trimLeft) {
String.prototype.trimLeft = function() {
return this.replace(/^\s+/,'');
};
String.prototype.trimRight = function() {
return this.replace(/\s+$/,'');
};
if (!''.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}
}
이제 일 [스트라이스트림 ()] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) 가 기본 Javascript 구축현 사용할 수 있습니다.
var orig = " foo ";
console.log(orig.trim());//foo
참조
String.prototype.trim = String.prototype.trim || function () {
return this.replace(/^\s+|\s+$/g, "");
};
String.prototype.trimLeft = String.prototype.trimLeft || function () {
return this.replace(/^\s+/, "");
};
String.prototype.trimRight = String.prototype.trimRight || function () {
return this.replace(/\s+$/, "");
};
String.prototype.trimFull = String.prototype.trimFull || function () {
return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, "").replace(/\s+/g, " ");
};
뻔뻔하게 도둑맞았다구 에서 맷 두에레크.
[각 js] [1] 에서 코드 트리밍할 프로젝트
var trim = (function() {
// if a reference is a `String`.
function isString(value){
return typeof value == 'string';
}
// native trim is way faster: http://jsperf.com/angular-trim-test
// but IE doesn't have it... :-(
// TODO: we should move this into IE/ES5 polyfill
if (!String.prototype.trim) {
return function(value) {
return isString(value) ?
value.replace(/^\s*/, '').replace(/\s*$/, '') : value;
};
}
return function(value) {
return isString(value) ? value.trim() : value;
};
})();
이를 '트리밍할, 콜 (". '누구없어요 ")
[1]: https://github.com/angular/angular.js/blob/master/src/Angular.js # L455
간단히 코드를 사용하여
var str = " Hello World! ";
alert(str.trim());
브라우저 지원
Feature Chrome Firefox Internet Explorer Opera Safari Edge
Basic support (Yes) 3.5 9 10.5 5 ?
늙은 브라우저 추가 프로토타입
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
오늘날 거의 모든 브라우저는 '스트린고드프로토티페스트림 ()' .
이 같은 서비스를 사용할 수 있습니다.
var origStr = ' foo ';
var newStr = origStr.trim(); // Value of newStr becomes 'foo'
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
내가 쓴 적이 있는 경우 이 기능을 이용할 수 없었다 () 함수를 트리밍할 스트림 JS way back (2008년). 아직도 일부 오래된 브라우저 () 함수를 호출하고 스트림 이 기능을 지원하지 못하는 사람을 도울 수 있으면 좋겠다.
function trim(str)
{
var startpatt = /^\s/;
var endpatt = /\s$/;
while(str.search(startpatt) == 0)
str = str.substring(1, str.length);
while(str.search(endpatt) == str.length-1)
str = str.substring(0, str.length-1);
return str;
}
이 함수를 호출할 수 다음과 같은 방법으로 예로 들 수 있습니다.
form.elements[i].value = trim(form.elements[i].value);