18

There are a lot of scripts out there to trim a string in JavaScript, but none that allow you to just left trim a string.

This is what I use to trim:

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

I would like to change this a little and create a new function called leftTrim that only removes the leading space.

jamesmhaley
  • 44,484
  • 11
  • 36
  • 49
  • JavaScript now has `trimStart()` and `trimEnd()`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStarttrimEnd() • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd – sideshowbarker Apr 09 '21 at 07:10

8 Answers8

31

Use:

String.prototype.leftTrim = function() {
    return this.replace(/^\s+/,"");
}

In the regex the:

  • ^ means "from the beginning of the string"
  • \s means whitespace character class
  • + means one-or more (greedy)

so....

  • ^\s+ means "one or more consecutive whitespace characters from the beginning of the class"

Note: The g flag at the end of your regex is unnecessary as the anchors (^ and $) explicitly define what will match. There cannot be multiple matches.

See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp for details on regex syntax in javascript

Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
9

to keep this updated:

ES 10:

with ES10 comes the trimStart functionality.

a synonym for it is trimLeft.

const greeting = '   Hi!   ';
console.log(greeting); 
// "   Hi!   "
console.log(greeting.trimStart()); 
// "Hi!   "
console.log(greeting.trimLeft()); 
// "Hi!   "

ES6:

an ES6 version could be:

function trimLeft(string) {
  const first = [...string].findIndex(char => char !== ' ');
  return string.substring(first, string.length);
}

console.log(trimLeft("   Hi!   ", chars));
// "Hi!   "


Robin F.
  • 1,137
  • 11
  • 19
  • I would suggest simply adding a `trimStart` polyfill to String in ES 6, instead of having function with different name. More future proof. – Franklin Yu Mar 18 '19 at 02:46
  • @FranklinYu that really depends on the coding paradigms used within the project. programming functionally you would do exactly the opposite and wrap the function on the string prototype to make it composable. – Robin F. Mar 18 '19 at 08:29
5

I've already answered a similar question just a few moments ago, but here's my solution to your question.

String.prototype.trimLeft = String.prototype.trimLeft || function () {
    var start = -1;

    while( this.charCodeAt(++start) < 33 );

    return this.slice( start, this.length);
};

The above solution is based on Ariel Flesler fast trim function and the fact that Firefox 3.5 and above has a built-in trimLeft method on the String object.

Community
  • 1
  • 1
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • 1
    There is a [stage 2 proposal](https://github.com/tc39/proposal-string-left-right-trim) that renamed the method to `trimStart`. – Franklin Yu Apr 18 '18 at 16:50
2
String.prototype.leftTrim = function() {
        return this.replace(/^\s+/,"");
}
igor
  • 2,090
  • 20
  • 32
2

Very simple, the regex needs a small change:

String.prototype.leftTrim = function() {
    return this.replace(/^\s+/,"");
}

See also:

Jesper
  • 202,709
  • 46
  • 318
  • 350
2

From the ECMAScript2015 (ES6) you can use the Built-In function String.prototype.trimStart()

var str1 = "    Lorem ipsum dolor sit amet    ";
var str2 = "Lorem ipsum dolor sit amet    ";
var str3 = "    Lorem ipsum dolor sit amet";
var str4 = "Lorem ipsum dolor sit amet";

console.log(str1.trimStart());
console.log(str2.trimStart());
console.log(str3.trimStart());
console.log(str4.trimStart());

Check browser compatibility here: https://developer.mozilla.org

Blackjack
  • 1,322
  • 1
  • 16
  • 21
1

First Way -

   String.prototype.leftTrim = function() {
    return this.replace(/^[\s]*/,"");
    }

Second Way -
Using the trimStart functionality.

    var fruit = "    apple";
    console.log(fruit.trimStart());
Hrithik
  • 11
  • 1
0

Most easiest way

var dog = "    Hi    ";
var keywordvalue = (dog.trimLeft());
alert(keywordvalue);

https://www.codehaven.co.uk/javascript/trim-left-javascript-text/

Mikeys4u
  • 1,494
  • 18
  • 26