For AI agents: the complete documentation index is available at /tc39-atlas/en/llms.txt, the full documentation bundle is available at /tc39-atlas/en/llms-full.txt, and this page is available as Markdown at /tc39-atlas/en/proposals/stage/0/proposal-string-trim-characters.md.
  • English
  • String trim characters S0

    Proposal details
    Proposal overview

    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.

    Note

    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
    const str = "-_-abc-_-";
    const characters = "-_";
    
    const regex = new RegExp(`(^[${characters}]*)|([${characters}]*$)`, 'g')
    
    console.log(str.replaceAll(regex, '')) // abc
    • manually
    const str = "-_-abc-_-";
    const characters = "-_";
    
    let start = 0;
    while (characters.indexOf(str[start]) >= 0) {
        start += 1;
    }
    let end = str.length - 1;
    while (characters.indexOf(str[end]) >= 0) {
        end -= 1;
    }
    
    console.log(str.substr(start, end - start + 1)) // abc

    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:

    interface String {
        trim(characters?: string): string;
        trimStart(characters?: string): string;
        trimEnd(characters?: string): string;
    }
    

    With this proposal, we could use as:

    const str = "-_-abc-_-";
    const characters = "-_";
    
    console.log(str.trim(characters)) // abc
    console.log(str.trimStart(characters)) // abc-_-
    console.log(str.trimEnd(characters)) // -_-abc

    Prior art

    Previous discuss

    Proposer

    Champions:

    • @Kingwl (Wenlu Wang, KWL)