From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../global_objects/regexp/@@split/index.html | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 files/ru/web/javascript/reference/global_objects/regexp/@@split/index.html (limited to 'files/ru/web/javascript/reference/global_objects/regexp/@@split') diff --git a/files/ru/web/javascript/reference/global_objects/regexp/@@split/index.html b/files/ru/web/javascript/reference/global_objects/regexp/@@split/index.html new file mode 100644 index 0000000000..8a0e07c804 --- /dev/null +++ b/files/ru/web/javascript/reference/global_objects/regexp/@@split/index.html @@ -0,0 +1,113 @@ +--- +title: 'RegExp.prototype[@@split]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@split +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@split +--- +
{{JSRef}}
+ +

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

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

Синтаксис

+ +
regexp[Symbol.split](str[, limit])
+ +

Параметры

+ +
+
str
+
Цель разбиения.
+
limit
+
+

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

+
+
+ +

Возвращаемое значение

+ +

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

+ +

Описание

+ +

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

+ +
'a-b-c'.split(/-/);
+
+/-/[Symbol.split]('a-b-c');
+ +

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

+ +

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

+ +

Примеры

+ +

Прямой вызов

+ +

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

+ +
var re = /-/g;
+var str = '2016-01-02';
+var result = re[Symbol.split](str);
+console.log(result);  // ["2016", "01", "02"]
+
+ +

Использование @@split в подклассах

+ +

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

+ +
class MyRegExp extends RegExp {
+  [Symbol.split](str, limit) {
+    var result = RegExp.prototype[Symbol.split].call(this, str, limit);
+    return result.map(x => "(" + 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)"]
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-regexp.prototype-@@split', 'RegExp.prototype[@@split]')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-regexp.prototype-@@split', 'RegExp.prototype[@@split]')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ +
+ + +

{{Compat("javascript.builtins.RegExp.@@split")}}

+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf