es.string.match.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
  3. var anObject = require('../internals/an-object');
  4. var toLength = require('../internals/to-length');
  5. var requireObjectCoercible = require('../internals/require-object-coercible');
  6. var advanceStringIndex = require('../internals/advance-string-index');
  7. var regExpExec = require('../internals/regexp-exec-abstract');
  8. // @@match logic
  9. fixRegExpWellKnownSymbolLogic('match', 1, function (MATCH, nativeMatch, maybeCallNative) {
  10. return [
  11. // `String.prototype.match` method
  12. // https://tc39.es/ecma262/#sec-string.prototype.match
  13. function match(regexp) {
  14. var O = requireObjectCoercible(this);
  15. var matcher = regexp == undefined ? undefined : regexp[MATCH];
  16. return matcher !== undefined ? matcher.call(regexp, O) : new RegExp(regexp)[MATCH](String(O));
  17. },
  18. // `RegExp.prototype[@@match]` method
  19. // https://tc39.es/ecma262/#sec-regexp.prototype-@@match
  20. function (regexp) {
  21. var res = maybeCallNative(nativeMatch, regexp, this);
  22. if (res.done) return res.value;
  23. var rx = anObject(regexp);
  24. var S = String(this);
  25. if (!rx.global) return regExpExec(rx, S);
  26. var fullUnicode = rx.unicode;
  27. rx.lastIndex = 0;
  28. var A = [];
  29. var n = 0;
  30. var result;
  31. while ((result = regExpExec(rx, S)) !== null) {
  32. var matchStr = String(result[0]);
  33. A[n] = matchStr;
  34. if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
  35. n++;
  36. }
  37. return n === 0 ? null : A;
  38. }
  39. ];
  40. });