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 September 2015.

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
Writableno
Enumerableno
Configurableno

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® 2025 Language Specification
# sec-symbol.split

Compatibilidade com navegadores

BCD tables only load in the browser

Veja também