Symbol.split 指向一个正则表达式的索引处分割字符串的方法。这个方法通过 String.prototype.split() 调用。
详情请参阅 RegExp.prototype[Symbol.split]() 和 String.prototype.split()。
{{InteractiveExample("JavaScript Demo: Symbol.split", "taller")}}
```js interactive-example
class Split1 {
constructor(value) {
this.value = value;
}
Symbol.split {
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"
## 值
内置通用符号 `Symbol.split`。
{{js_property_attributes(0, 0, 0)}}
## 示例
### 自定义反向分割
```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" ]