trim()
메서드는 문자열 양 끝의 공백을 제거합니다. 공백이란 모든 공백문자(space, tab, NBSP 등)와 모든 개행문자(LF, CR 등)를 의미합니다.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
구문
str.trim()
반환 값
양 끝에서 공백을 제거한 새로운 문자열.
설명
trim()
메서드는 양끝의 공백을 제거한 문자열을 반환합니다. trim()
은 원본 문자열에는 영향을 주지 않습니다.
예제
trim()
사용
아래의 예제는 소문자 문자열 'foo'
를 표시합니다.
var orig = ' foo ';
console.log(orig.trim()); // 'foo'
// 한 쪽의 공백만 제거하는 .trim() 예제
var orig = 'foo ';
console.log(orig.trim()); // 'foo'
폴리필
다른 코드 전에 아래 코드를 실행하면 지원하지 않는 환경에서도 String.trim()
을 사용할 수 있습니다.
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
명세
표준 | 상태 | 비고 |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'String.prototype.trim' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.8.1. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.trim' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'String.prototype.trim' in that specification. |
Living Standard |
브라우저 호환성
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.