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 regExpExec = require('../internals/regexp-exec-abstract');
9 fixRegExpWellKnownSymbolLogic('search', 1, function (SEARCH, nativeSearch, maybeCallNative) {
11 // `String.prototype.search` method
12 // https://tc39.es/ecma262/#sec-string.prototype.search
13 function search(regexp) {
14 var O = requireObjectCoercible(this);
15 var searcher = regexp == undefined ? undefined : regexp[SEARCH];
16 return searcher !== undefined ? searcher.call(regexp, O) : new RegExp(regexp)[SEARCH](String(O));
18 // `RegExp.prototype[@@search]` method
19 // https://tc39.es/ecma262/#sec-regexp.prototype-@@search
21 var res = maybeCallNative(nativeSearch, regexp, this);
22 if (res.done) return res.value;
24 var rx = anObject(regexp);
27 var previousLastIndex = rx.lastIndex;
28 if (!sameValue(previousLastIndex, 0)) rx.lastIndex = 0;
29 var result = regExpExec(rx, S);
30 if (!sameValue(rx.lastIndex, previousLastIndex)) rx.lastIndex = previousLastIndex;
31 return result === null ? -1 : result.index;