Jun 29, 2021
trimStart() function is what you're looking for.
Equivalently, you can call
trimLeft(), which is an alias for
let example = ' Hello World'; example.trimStart(); // 'Hello World' example.trimLeft(); // 'Hello World'
trimStart() in Internet Explorer or Node.js < 10.0.0.
An alternative is to use the string
replace() function with a regular expression.
// \s is a metacharacter representing any whitespace character // See https://www.w3schools.com/jsref/jsref_regexp_whitespace.asp example.replace(/^\s+/, ''); // 'Hello World'
Trimming Other Characters
You can also use
replace() to remove any other set of characters from the beginning of the string.
For example, suppose you want to remove any leading 'Na ' strings.
You can use the regular expression
^ means at the beginning of the string,
(Na) means the group
+ means one or more.
let example = 'Na Na Na Na Na Na Na Na Na Na Na Na Na Na Na Na BATMAN!'; example.replace(/^(Na )+/, ''); // 'BATMAN!'