Os nossos voluntários ainda não traduziram este artigo para Português (Europeu) . Junte-se a nós e ajude-nos a fazer o trabalho!
The trim()
method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
Syntax
str.trim()
Return value
A new string representing the calling string stripped of whitespace from both ends.
Description
The trim()
method returns the string stripped of whitespace from both ends. trim()
does not affect the value of the string itself.
Examples
Using trim()
The following example displays the lowercase string 'foo'
:
var orig = ' foo '; console.log(orig.trim()); // 'foo' // Another example of .trim() removing whitespace from just one side. var orig = 'foo '; console.log(orig.trim()); // 'foo'
Polyfill
Running the following code before any other code will create trim()
if it's not natively available.
if (!String.prototype.trim) { String.prototype.trim = function () { return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }; }
Specifications
Specification | Status | Comment |
---|---|---|
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 2017 Draft (ECMA-262) The definition of 'String.prototype.trim' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | 3.5 (1.9.1) | 9 | 10.5 | 5 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
See also
Etiquetas do documento e colaboradores
Etiquetas:
Colaboradores para esta página:
eduardoboucas,
fscholz,
alexlur,
madarche,
cliffordfajardo,
Mingun,
spender,
stevemao,
john.watts,
siberiawolf0307,
George,
mitar,
Brettz9,
Sheppy,
ethertank,
Ruakh,
ziyunfei,
Nesk,
evilpie,
Potappo,
Midnightaz,
Chris Chittleborough
Última atualização por:
eduardoboucas,