Symbol.split - JavaScript | MDN

Probieren Sie es aus

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"

Wert

Das bekannte Symbol Symbol.split.

Eigenschaften von Symbol.split
Schreibbarnein
Aufzählbarnein
Konfigurierbarnein

Beispiele

Benutzerdefiniertes Rückwärts-Split

js

class ReverseSplit {
  [Symbol.split](string) {
    const array = string.split(" ");
    return array.reverse();
  }
}

console.log("Another one bites the dust".split(new ReverseSplit()));
// [ "dust", "the", "bites", "one", "Another" ]

Spezifikationen

Spezifikation
ECMAScript® 2026 Language Specification
# sec-symbol.split

Browser-Kompatibilität

Siehe auch

Help improve MDN

Erfahren Sie, wie Sie beitragen können Diese Seite wurde automatisch aus dem Englischen übersetzt.