For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/proposal-string-left-right-trim.md.
  • 简体中文
  • String.prototype.{trimStart,trimEnd} S4

    中文标题:String.prototype.{trimStart, trimEnd}

    提案概览
    提案速览

    该提案旨在标准化 String.prototype.trimStart 和 trimEnd 方法,它们分别移除字符串开头或结尾的空白字符。它还引入了 trimLeft 和 trimRight 作为别名以确保 Web 兼容性,并匹配 padStart/padEnd 的命名模式。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    String.prototype.trimStart / String.prototype.trimEnd

    ECMAScript 提案,针对 String.prototype.trimStart/trimEnd(以及 trimLeft/trimRight)的规范、测试和参考实现。

    阶段 4

    此提案已完成,并已合并到 ECMA262 规范 中。请在此查看规范文本。

    理由

    ES5 已标准化了 String.prototype.trim。所有主流引擎也已实现了对应的 trimLefttrimRight 函数——但没有任何标准规范。 为了与 padStart/padEnd 保持一致,我们提议将 trimStarttrimEnd 作为标准函数,并将 trimLeft/trimRight 作为其别名,以满足 Web 兼容性需求。

    规范

    您可以在 ecmarkup 中查看规范,或查看渲染后的 HTML

    命名 / 别名

    为了与 padStart/padEnd 保持一致,标准函数将命名为 trimStarttrimEnd,但为了 Web 兼容性,trimLeft 将作为 trimStart 的别名,trimRight 将作为 trimEnd 的别名。这意味着在大多数引擎中,String.prototype.trimRight.name 将从 "trimRight" 改为 "trimEnd"。规范作者预计这不会导致任何破坏。

    Test262 覆盖

    PR 已开启

    本提案的状态

    此初始提案由 @sebmarkbage 起草,更新的规范由 @evilpie 草拟,并得到了 @ljharb 的输入。

    此提案目前处于 流程第 4 阶段

    指定的 TC39 审查者:Jordan Harband + Daniel Ehrenberg

    实现