--- title: String.prototype.trim() slug: Web/JavaScript/Reference/Global_Objects/String/Trim tags: - ECMAScript 5 - JavaScript - Method - Prototype - Reference - String translation_of: Web/JavaScript/Reference/Global_Objects/String/Trim ---
trim()
메서드는 문자열 양 끝의 공백을 제거합니다. 공백이란 모든 공백문자(space, tab, NBSP 등)와 모든 개행문자(LF, CR 등)를 의미합니다.
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, ''); }; }
표준 | 상태 | 비고 |
---|---|---|
{{SpecName('ES5.1', '#sec-15.5.4.20', 'String.prototype.trim')}} | {{Spec2('ES5.1')}} | Initial definition. Implemented in JavaScript 1.8.1. |
{{SpecName('ES6', '#sec-string.prototype.trim', 'String.prototype.trim')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-string.prototype.trim', 'String.prototype.trim')}} | {{Spec2('ESDraft')}} |
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.
{{Compat("javascript.builtins.String.trim")}}