The [@@split]() method of RegExp instances specifies how String.prototype.split should behave when the regular expression is passed in as the separator.
The [@@split]() method of RegExp instances specifies how String.prototype.split should behave when the regular expression is passed in as the separator.
regexp[Symbol.split](str) regexp[Symbol.split](str, limit)
strThe target of the split operation.
limit Optional
Integer specifying a limit on the number of splits to be found. The [@@split]() method still splits on every match of this RegExp pattern (or, in the Syntax above, regexp), until the number of split items match the limit or the string falls short of this pattern.
An Array containing substrings as its elements. Capturing groups are included.
This method is called internally in String.prototype.split() when a RegExp is passed as the separator. For example, the following two examples return the same result.
"a-b-c".split(/-/); /-/[Symbol.split]("a-b-c");
This method exists for customizing the behavior of split() in RegExp subclasses.
The RegExp.prototype[@@split]() base method exhibits the following behaviors:
@@species to construct a new regexp, thus avoiding mutating the original regexp in any way.g ("global") flag is ignored, and the y ("sticky") flag is always applied even when it was not originally present./a?/), an empty array is returned. Otherwise, if the regexp can't match an empty string, [""] is returned.this.exec(). Since the regexp is always sticky, this will move along the string, each time yielding a matching string, index, and any capturing groups.lastIndex would still be advanced — if the regex is Unicode-aware, it would advance by one Unicode code point; otherwise, it advances by one UTF-16 code unit.limit parameter, if provided, while trying to be as close as possible. Therefore, the last match and its capturing groups may not all be present in the returned array if the array is already filled. This method can be used in almost the same way as String.prototype.split(), except the different this and the different order of arguments.
const re = /-/g; const str = "2016-01-02"; const result = re[Symbol.split](str); console.log(result); // ["2016", "01", "02"]
Subclasses of RegExp can override the [@@split]() method to modify the default behavior.
class MyRegExp extends RegExp { [Symbol.split](str, limit) { const result = RegExp.prototype[Symbol.split].call(this, str, limit); return result.map((x) => `(${x})`); } } const re = new MyRegExp("-"); const str = "2016-01-02"; const result = str.split(re); // String.prototype.split calls re[@@split]. console.log(result); // ["(2016)", "(01)", "(02)"]
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
@@split |
50 | 79 | 49 | 37 | 10 | 50 | 49 | 37 | 10 | 5.0 | 50 | 1.0 | 6.0.0 | |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@split