1 | 'use strict';
|
---|
2 | var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
|
---|
3 | var anObject = require('../internals/an-object');
|
---|
4 | var requireObjectCoercible = require('../internals/require-object-coercible');
|
---|
5 | var sameValue = require('../internals/same-value');
|
---|
6 | var toString = require('../internals/to-string');
|
---|
7 | var regExpExec = require('../internals/regexp-exec-abstract');
|
---|
8 |
|
---|
9 | // @@search logic
|
---|
10 | fixRegExpWellKnownSymbolLogic('search', function (SEARCH, nativeSearch, maybeCallNative) {
|
---|
11 | return [
|
---|
12 | // `String.prototype.search` method
|
---|
13 | // https://tc39.es/ecma262/#sec-string.prototype.search
|
---|
14 | function search(regexp) {
|
---|
15 | var O = requireObjectCoercible(this);
|
---|
16 | var searcher = regexp == undefined ? undefined : regexp[SEARCH];
|
---|
17 | return searcher !== undefined ? searcher.call(regexp, O) : new RegExp(regexp)[SEARCH](toString(O));
|
---|
18 | },
|
---|
19 | // `RegExp.prototype[@@search]` method
|
---|
20 | // https://tc39.es/ecma262/#sec-regexp.prototype-@@search
|
---|
21 | function (string) {
|
---|
22 | var rx = anObject(this);
|
---|
23 | var S = toString(string);
|
---|
24 | var res = maybeCallNative(nativeSearch, rx, S);
|
---|
25 |
|
---|
26 | if (res.done) return res.value;
|
---|
27 |
|
---|
28 | var previousLastIndex = rx.lastIndex;
|
---|
29 | if (!sameValue(previousLastIndex, 0)) rx.lastIndex = 0;
|
---|
30 | var result = regExpExec(rx, S);
|
---|
31 | if (!sameValue(rx.lastIndex, previousLastIndex)) rx.lastIndex = previousLastIndex;
|
---|
32 | return result === null ? -1 : result.index;
|
---|
33 | }
|
---|
34 | ];
|
---|
35 | });
|
---|