How to Trim Characters from a String in JavaScript

Oct 6, 2021

To trim leading and trailing whitespace from a string in JavaScript, you should use the String.prototype.trim() method. The trim() method removes leading and trailing whitespace characters, including tabs and newlines.

'\t  Hello, World\t   \n\n'.trim(); // 'Hello, World'

The trim() method is especially useful with template strings, because template strings end up with leading and trailing newlines if you put ` on a separate line.

const code = `
console.log('Hello, World!);
`;

code; // "\nconsole.log('Hello, World!);\n"
code.trim(); // "console.log('Hello, World!)"

Trim Arbitrary Characters

To trim arbitrary characters, you should use the replace() function. replace() takes two arguments:

By using ^ (start of string) and $ (end of string), you can create two replace() calls that replace leading and trailing instances of a character as shown below.

let example = 'aaaaaaaaaaaaaaaaaaaaBBBaaaaaaaaaaaaaaaaaaaa';
example.replace(/^a+/, '').replace(/a+$/, ''); // BBB

Trim Multiple Characters

To trim multiple characters, broaden the search of the regular expression.

let example = '1234567890 Mastering JS 1234567890';
example.replace(/^[0-9]+/, '').replace(/[0-9]+$/, ''); // Mastering JS
example.replace(/[0-9]+/, ''); // Mastering JS

trimStart() and trimEnd()

JavaScript strings also have trimStart() and trimEnd() methods. The trimStart() function removes all leading whitespace, and trimEnd() removes all trailing whitespace. In other words, str.trimStart().trimEnd() is equivalent to str.trim().

let str = ' hello world ';
str.trimStart(); // 'hello world '
str.trimEnd(); // ' hello world'

Keep in mind that trimStart() and trimEnd() are relatively new additions to JavaScript, introduced in ES2019. They are not supported in Node.js before v10.x, and they are not supported in any version of Internet Explorer. While they are safe to use in Node.js, you may want to add a polyfill for browser apps if you need to support IE.


Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials