Symbol.split
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since janeiro de 2020.
O Symbol.split é um símbolo conhecido que especifica o método que divide uma string nos índices correspondentes a uma expressão regular. Essa função é chamada pelo método String.prototype.split().
Para mais informações, veja RegExp.prototype[@@split]() e String.prototype.split().
Experimente
class Split1 { constructor(value) { this.value = value; } [Symbol.split](string) { const index = string.indexOf(this.value); return `${this.value}${string.substring(0, index)}/${string.substring( index + this.value.length, )}`; } } console.log("foobar".split(new Split1("foo"))); // Expected output: "foo/bar" Property attributes of Symbol.split | |
|---|---|
| Writable | no |
| Enumerable | no |
| Configurable | no |
Exemplos
>Divisão reversa personalizada
js
class ReverseSplit { [Symbol.split](string) { const array = string.split(" "); return array.reverse(); } } console.log("Another one bites the dust".split(new ReverseSplit())); // resultado esperado: [ "dust", "the", "bites", "one", "Another" ] Especificações
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-symbol.split> |