45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
|
'use strict';
|
||
|
var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
|
||
|
var anObject = require('../internals/an-object');
|
||
|
var toLength = require('../internals/to-length');
|
||
|
var requireObjectCoercible = require('../internals/require-object-coercible');
|
||
|
var advanceStringIndex = require('../internals/advance-string-index');
|
||
|
var regExpExec = require('../internals/regexp-exec-abstract');
|
||
|
|
||
|
// @@match logic
|
||
|
fixRegExpWellKnownSymbolLogic('match', function (MATCH, nativeMatch, maybeCallNative) {
|
||
|
return [
|
||
|
// `String.prototype.match` method
|
||
|
// https://tc39.es/ecma262/#sec-string.prototype.match
|
||
|
function match(regexp) {
|
||
|
var O = requireObjectCoercible(this);
|
||
|
var matcher = regexp == undefined ? undefined : regexp[MATCH];
|
||
|
return matcher !== undefined ? matcher.call(regexp, O) : new RegExp(regexp)[MATCH](String(O));
|
||
|
},
|
||
|
// `RegExp.prototype[@@match]` method
|
||
|
// https://tc39.es/ecma262/#sec-regexp.prototype-@@match
|
||
|
function (string) {
|
||
|
var res = maybeCallNative(nativeMatch, this, string);
|
||
|
if (res.done) return res.value;
|
||
|
|
||
|
var rx = anObject(this);
|
||
|
var S = String(string);
|
||
|
|
||
|
if (!rx.global) return regExpExec(rx, S);
|
||
|
|
||
|
var fullUnicode = rx.unicode;
|
||
|
rx.lastIndex = 0;
|
||
|
var A = [];
|
||
|
var n = 0;
|
||
|
var result;
|
||
|
while ((result = regExpExec(rx, S)) !== null) {
|
||
|
var matchStr = String(result[0]);
|
||
|
A[n] = matchStr;
|
||
|
if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
|
||
|
n++;
|
||
|
}
|
||
|
return n === 0 ? null : A;
|
||
|
}
|
||
|
];
|
||
|
});
|