aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/javascript/reference/global_objects/regexp/@@split/index.html
blob: fffdadf7c90e092c2a86ba242c83fa5d06228a45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: 'RegExp.prototype[@@split]()'
slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@split
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@split
---
<div>{{JSRef}}</div>

<p><strong><code>[@@split]()</code></strong> метод делит объект {{jsxref("String")}}  в массив строк, путём разбиения строки на подстроки.</p>

<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@split.html")}}</div>



<h2 id="Синтаксис">Синтаксис</h2>

<pre class="syntaxbox"><var>regexp</var>[Symbol.split](str[, <var>limit</var>])</pre>

<h3 id="Параметры">Параметры</h3>

<dl>
 <dt><code>str</code></dt>
 <dd>Цель разбиения.</dd>
 <dt><code>limit</code></dt>
 <dd>
 <p>Необязательное. Целое число ограничивающее кол-во разбиений. <code>[@@split]()</code> метод разбивает все совпадения <code>this</code> RegExp шаблона, до тех пор пока не достигнет числа <code>limit</code> или строка будет короче <code>this</code> шаблона.</p>
 </dd>
</dl>

<h3 id="Возвращаемое_значение">Возвращаемое значение</h3>

<p>{{jsxref("Array")}} содержит подстроки как элементы.</p>

<h2 id="Описание">Описание</h2>

<p>Этот метод вызывает {{jsxref("String.prototype.split()")}}, если аргумент <code>separator</code> объект {{jsxref("RegExp")}}. Для примера, два данных выражения возвращают одинаковый результат.</p>

<pre class="brush: js">'a-b-c'.split(/-/);

/-/[Symbol.split]('a-b-c');</pre>

<p>Этот метод существует для кастомизации поведения (разбиения) подкласса <code>RegExp</code>.</p>

<p>Если аргумент <code>str</code> <strong>не </strong>объект типа {{jsxref("RegExp")}}, метод {{jsxref("String.prototype.split()")}} не вызывается, так же не создаётся объект типа {{jsxref("RegExp")}}.</p>

<h2 id="Примеры">Примеры</h2>

<h3 id="Прямой_вызов">Прямой вызов</h3>

<p>Этот метод может быть использован, так же как {{jsxref("String.prototype.split()")}}, кроме случаев когда <code>this</code> отличаются и аргументы идут в разном порядке.</p>

<pre class="brush: js">var re = /-/g;
var str = '2016-01-02';
var result = re[Symbol.split](str);
console.log(result);  // ["2016", "01", "02"]
</pre>

<h3 id="Использование_split_в_подклассах">Использование <code>@@split</code> в подклассах</h3>

<p>Подклассы {{jsxref("RegExp")}} могут переопределить <code>[@@split]()</code> для изменения стандартного поведения.</p>

<pre class="brush: js">class MyRegExp extends RegExp {
  [Symbol.split](str, limit) {
    var result = RegExp.prototype[Symbol.split].call(this, str, limit);
    return result.map(x =&gt; "(" + x + ")");
  }
}

var re = new MyRegExp('-');
var str = '2016-01-02';
var result = str.split(re); // String.prototype.split calls re[@@split].
console.log(result); // ["(2016)", "(01)", "(02)"]
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-regexp.prototype-@@split', 'RegExp.prototype[@@split]')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@split', 'RegExp.prototype[@@split]')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>


<p>{{Compat("javascript.builtins.RegExp.@@split")}}</p>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("String.prototype.split()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li>
 <li>{{jsxref("RegExp.prototype.exec()")}}</li>
 <li>{{jsxref("RegExp.prototype.test()")}}</li>
</ul>