Skip to content

About

TC39 proposal to customize string trimming

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

4 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

Trim Specific Characters

  • Stage: 1
  • Champion: Eemeli Aro
  • Presentations:
    • 2026.11: For Stage 1 or 2 (slides)

When processing String values from user input or otherwise, needing to trim the String of its outer whitespace or other characters is a very common need. JavaScript supports this with the String.prototype.trim, .trimStart, and .trimEnd functions.

However, currently String trimming is restricted to only trimming white space, which is defined as:

The definition of white space is the union of WhiteSpace and LineTerminator.

where the WhiteSpace definition includes

any code point in general category “Space_Separator”

This is somewhat problematic for at least two reasons:

  1. The set of trimmed characters may change when the Unicode standard is updated. This is rare, but does happen; the most recent such change was made in Unicode 6.3 (2013), when MONGOLIAN VOWEL SEPARATOR U+180E was re-classified as a formatting character instead of a space character.

  2. The set of trimmed characters may not match user expectations, in particular when parsing syntax that has a different definition for "white space". For example, JSONWhiteSpace only allows for \r, \n, \t, and space characters as white space. Using String.prototype.trim when parsing JSON would lead to treating additional characters as white space.

We should allow for the set of trimmed characters to be user-customizable.

Use Cases

Whitespace trimming is a very common operation when parsing a value from a string. All well-defined syntaxes for formats used for interchange are explicit about their definitions of whitespace, for example:

  • HTML: \r, \n, \f, \t, and space
  • JSON and XML: \r, \n, \t, and space
  • Java Properties: \f, \t, and space
  • YAML and TOML: \t and space

When parsing these formats with JavaScript, using .trim() for string trimming is currently not possible.

Beyond whitespace, strings that are processed in JavaScript often contain outer characters that need to be trimmed. This is needed for the normalization of string values, such as removing trailing slashes from a URL path, or, as requested in proposal-string-left-right-trim#22, trimming a leading ?.

Customizable Trimming Elsewhere

At least the following languages' standard libraries allow for string trimming to be customized:

Language Function Argument type
Common Lisp string-trim sequence of characters
C# String.Trim character or array of characters
Go strings.Trim string of characters
PHP trim string of characters
Python str.strip string of characters
Rust str.trim_matches a char, a slice of chars, or a function or closure that determines if a character matches

Proposed Solution

Add an optional characters argument to the String.prototype.trim, .trimStart, and .trimEnd functions. If present, this would be required to be a String of characters that would be trimmed from the start and/or end of the string:

"\n\n foo".trim("\r\n"); // ' foo'
"bar:: ".trimEnd(" :"); // 'bar'

"bar:: ".trimEnd([" ", ":"]); // TypeError

Alternatives

It is already now possible to trim strings using regular expressions, but this is not very ergonomic. For example, compare:

jsonValue.replace(/^[\r\n\t ]+|[\r\n\t ]+$/g, "");
jsonValue.trim("\r\n\t ");

The .trim() argument could accept an iterable of characters instead of a String, which would allow for e.g. [" ", ":"] to be used as a value. When iterating, we would then need to check that each value consists of a single code point, which seems like an unnecessary complication.

Rather than adding an argument to the existing functions, we could add new String functions like .trimMatches(), .trimStartMatches(), and .trimEndMatches() which would require the argument. However, this seems quite unnecessary unless adding the argument turns out to be web-incompatible.

About

TC39 proposal to customize string trimming

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

4 watching

Forks

Used by

Contributors