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()

尝试一下

内置通用符号 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 Language Specification
# sec-symbol.split

浏览器兼容性

BCD tables only load in the browser

参阅