Trim spaces from start and end of string

Trim spaces from start and end of string

I am trying to find a way to trim spaces from the start and end of the title string. I was using this, but it doesn’t seem to be working:
title = title.replace(/(^[\s]+|[\s]+$)/g, ”);

Any ideas?


Solution 1:

Note: As of 2015, all major browsers (including IE>=9) support String.prototype.trim(). This means that for most use cases simply doing str.trim() is the best way of achieving what the question asks.

Steven Levithan analyzed many different implementation of trim in Javascript in terms of performance.

His recommendation is:

function trim1 (str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

for “general-purpose implementation which is fast cross-browser”, and

function trim11 (str) {
    str = str.replace(/^\s+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
    return str;

“if you want to handle long strings exceptionally fast in all browsers”.


Solution 2:

If using jQuery is an option:

 * Trim the site input[type=text] fields globally by removing any whitespace from the
 * beginning and end of a string on input .blur()

or simply:


Solution 3:

As @ChaosPandion mentioned, the String.prototype.trim method has been introduced into the ECMAScript 5th Edition Specification, some implementations already include this method, so the best way is to detect the native implementation and declare it only if it’s not available:

if (typeof String.prototype.trim != 'function') { // detect native implementation
  String.prototype.trim = function () {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');

Then you can simply:

title = title.trim();

Solution 4:

I know this is an old post, but just thought I’d share our solution. In the quest for shortest code (doesn’t everyone just love terse regex), one could instead use:

title = title.replace(/(^\s+|\s+$)/g, '');

BTW: I ran this same test through the link shared above — Faster JavaScript Trim and this pattern beat all the other HANDS down!

Using IE8, added test as test13. The results were:

Original length: 226002
trim1: 110ms (length: 225994)
trim2: 79ms (length: 225994)
trim3: 172ms (length: 225994)
trim4: 203ms (length:225994)
trim5: 172ms (length: 225994)
trim6: 312ms (length: 225994)
trim7: 203ms (length: 225994)
trim8: 47ms (length: 225994)
trim9: 453ms (length: 225994)
trim10: 15ms (length: 225994)
trim11: 16ms (length: 225994)
trim12: 31ms (length: 225994)
trim13: 0ms (length: 226002)

Solution 5:

ECMAScript 5 supports trim and this has been implemented in Firefox.

trim – MDC

Solution 6:

Here, this should do all that you need

function doSomething(input) {
    return input
              .replace(/^\s\s*/, '')     // Remove Preceding white space
              .replace(/\s\s*$/, '')     // Remove Trailing white space
              .replace(/([\s]+)/g, '-'); // Replace remaining white space with dashes

alert(doSomething("  something with  some       whitespace   "));