aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/javascript/reference/global_objects/string/trim/index.html
blob: 0f0b71f5487e25b1c2c183d71dd565962d193365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
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
---
<div>{{JSRef}}</div>

<p><strong><code>trim()</code></strong> 메서드는 문자열 양 끝의 공백을 제거합니다. 공백이란 모든 공백문자(space, tab, NBSP 등)와 모든 개행문자(LF, CR 등)를 의미합니다.</p>

<div>{{EmbedInteractiveExample("pages/js/string-trim.html")}}</div>



<h2 id="구문">구문</h2>

<pre class="syntaxbox"><var>str</var>.trim()</pre>

<h3 id="반환_값">반환 값</h3>

<p>양 끝에서 공백을 제거한 새로운 문자열.</p>

<h2 id="설명">설명</h2>

<p><code>trim()</code> 메서드는 양끝의 공백을 제거한 문자열을 반환합니다. <code>trim()</code>은 원본 문자열에는 영향을 주지 않습니다. </p>

<h2 id="예제">예제</h2>

<h3 id="trim()_사용"><code>trim()</code> 사용</h3>

<p>아래의 예제는 소문자 문자열  <code>'foo'</code>를 표시합니다.</p>

<pre class="brush: js">var orig = '   foo  ';
console.log(orig.trim()); // 'foo'

// 한 쪽의 공백만 제거하는 .trim() 예제

var orig = 'foo    ';
console.log(orig.trim()); // 'foo'
</pre>

<h2 id="폴리필">폴리필</h2>

<p>다른 코드 전에 아래 코드를 실행하면 지원하지 않는 환경에서도  <code>String.trim()</code> 을 사용할 수 있습니다.</p>

<pre class="brush: js">if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}
</pre>

<h2 id="명세">명세</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">표준</th>
   <th scope="col">상태</th>
   <th scope="col">비고</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.5.4.20', 'String.prototype.trim')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.8.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.trim', 'String.prototype.trim')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.trim', 'String.prototype.trim')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="브라우저_호환성">브라우저 호환성</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat("javascript.builtins.String.trim")}}</p>

<h2 id="참조">참조</h2>

<ul>
 <li>{{jsxref("String.prototype.trimLeft()")}} {{non-standard_inline}}</li>
 <li>{{jsxref("String.prototype.trimRight()")}} {{non-standard_inline}}</li>
</ul>