Languages
[Edit]
EN

JavaScript - string trim method example

8 points
Created by:
Wiktor-Sribiew
440

In this quick article, we are going to discuss how to trim string in JavaScript.

There are 3 different ways to trim string:

  • both side - most commonly used
  • left side
  • right side

Quick overlook:

// ONLINE-RUNNER:browser;

var text = '    abc    ';

// Both side trimming:
console.log(     'abc' === text.trim()                    ); // true
console.log(     'abc' === text.replace(/^\s*|\s*$/g, '') ); // true

// Left side trimming:
console.log( 'abc    ' === text.replace(/^\s*/g, '') ); // true

// Right side trimming:
console.log( '    abc' === text.replace(/\s*$/g, '') ); // true

Left and right side trimming with ECMAScript 2020:

// ONLINE-RUNNER:browser;

var text = '    abc    ';

// left side trimming:
console.log( 'abc    ' === text.trimStart() ); // true
console.log( 'abc    ' === text.trimLeft()  ); // true

// right side trimming:
console.log( '    abc' === text.trimEnd()   ); // true
console.log( '    abc' === text.trimRight() ); // true

 Note: trimStart, trimLeft, trimEnd, trimRight are in draft of ECMAScript 2020, so it is better to use replace approach until the methods will be commonly supported.

More detailed and grouped substring methods description is placed below.

1. Trim operation with built-in methods example

In this section two ways how to trim string in javascript are represented. One of them is based on String trim method. Second one replaces white characters with regular expression.

// ONLINE-RUNNER:browser;

var text = '    abc    ';

console.log( 'abc' === text.trim()                    ); // true
console.log( 'abc' === text.replace(/^\s*|\s*$/g, '') ); // true

2. Side trimming operation with built-in methods example

JavaScript API provides addtionally left and right triming.

2.1. Left side trimming

// ONLINE-RUNNER:browser;

var text = '    abc    ';

console.log( 'abc    ' === text.replace(/^\s*/g, '') ); // true
console.log( 'abc    ' === text.trimStart()          ); // true
console.log( 'abc    ' === text.trimLeft()           ); // true

Note: trimStart and trimLeft are in draft of ECMAScript 2020, so it is better to use replace approach until the methods will be commonly supported.

2.2. Right side trimming

// ONLINE-RUNNER:browser;

var text = '    abc    ';

console.log( '    abc' === text.replace(/\s*$/g, '') ); // true
console.log( '    abc' === text.trimEnd()            ); // true
console.log( '    abc' === text.trimRight()          ); // true

Note: trimEnd and trimRight are in draft of ECMAScript 2020, so it is better to use replace approach until the methods will be commonly supported.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!

JavaScript - String (documentation)

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join