[@@split]()
method splits a
String
object into an array of strings by separating the string into substrings.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
regexp[Symbol.split](str[, limit])
str
The target of the split operation.
limit
可选
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.
Array
containing substrings as its elements.
This method is called internally in
String.prototype.split()
if its
separator
argument is an object that has a
@@split
method, such as a
RegExp
. 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
subclass.
This method can be used in almost the same way as
String.prototype.split()
, except the different
this
and the different order of arguments.
let re = /-/g; let str = '2016-01-02'; let result = re[Symbol.split](str); console.log(result); // ["2016", "01", "02"]
@@split
in subclasses
Subclasses of
RegExp
can override the
[@@split]()
method to modify the default behavior.
class MyRegExp extends RegExp {
[Symbol.split](str, limit) {
let result = RegExp.prototype[Symbol.split].call(this, str, limit);
return result.map(x => "(" + x + ")");
}
}
let re = new MyRegExp('-');
let str = '2016-01-02';
let result = str.split(re); // String.prototype.split calls re[@@split].
console.log(result); // ["(2016)", "(01)", "(02)"]
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'RegExp.prototype[@@split]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@split
|
Chrome 50 | Edge 79 | Firefox 49 | IE No | Opera 37 | Safari 10 | WebView Android 50 | Chrome Android 50 | Firefox Android 49 | Opera Android 37 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持
String.prototype.split()
RegExp.prototype[@@match]()
RegExp.prototype[@@replace]()
RegExp.prototype[@@search]()
RegExp.prototype.exec()
RegExp.prototype.test()
RegExp
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.prototype.dotAll
RegExp.prototype.flags
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
RegExp.prototype.unicode
RegExp.rightContext ($')
RegExpInstance.lastIndex
get RegExp[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()