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 2020年1月.
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® 2026 Language Specification> # sec-symbol.split>  | 
            
浏览器兼容性
Loading…