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.
Symbol.split
指向一个正则表达式的索引处分割字符串的方法。这个方法通过 String.prototype.split()
调用。
详情请参阅 RegExp.prototype[Symbol.split]()
和 String.prototype.split()
。
尝试一下
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"
值
内置通用符号 Symbol.split
。
Symbol.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" ]
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-symbol.split |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
split |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.