String trim characters S0
- Stage: Stage 0
- Status: Active
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
The proposal adds an optional characters argument to String.prototype.trim, trimStart, and trimEnd, allowing developers to remove specified characters from the beginning and/or end of a string. Currently, only whitespace and line terminators can be stripped, so removing other characters requires regex or manual loops, which are less readable and may cause performance issues. The new API aims to provide a semantic and convenient way to express such operations.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
proposal-string-trim-characters
Proposal to add argument for .trim(), .trimStart() and .trimEnd() to allow strip the specified characters from strings.
Status
This proposal is a stage-0 proposal and waiting for feedback.
Motivation
We often remove some leading/trailing whitespaces from the beginning or end (or both) of a string by trim, trimStart, and trimEnd.
We can only remove the WhiteSpace and LineTerminator. If you want to remove some other strings who not defined by whitespace. There's no semantical and convenience way to do that.
Semantical & Convenience
The major point of the proposal is semantical and convenience.
For now. How could we remove some specific leading/trailing characters?
- regex
- manually
As you can see. For these both solutions, there's no idea about what does it doing when you see it, You have to pay attention on it to understand it.
Performance
And for the regex version, there's might also performance issue as TypeScript's implementations: jsbench.
Consolation
That's why we need this proposal. It's will add some semantic and convenience way to clearly representing the operation i want. And as a possible bonus, it also reduces the amount of very poorly performing code we write.
Core API
Add an optional argument characters into String.prototype.trim , String.prototype.trimStartand String.prototype.trimEnd.
This argument will allow us which characters will be remove from the start or end (or both) from the string.
The definition of API will looks like:
With this proposal, we could use as:
Prior art
- Lodash - lodash.trim, lodash.trimStart, lodash.trimEnd
- PHP - function.trim, function.ltrim, function.rtrim
- Python - str.strip, str.lstrip, str.rstrip
- C# - String.Trim, String.TrimStart, String.TrimEnd
- Go - Trim, TrimLeft, TrimRight, TrimPrefix, TrimSuffix
Previous discuss
- https://github.com/tc39/proposal-string-left-right-trim/issues/22
- https://esdiscuss.org/topic/string-trim-chars
- https://esdiscuss.org/topic/string-prototype-trimstart-string-prototype-trimend-with-a-given-string
Proposer
Champions:
- @Kingwl (Wenlu Wang, KWL)