regexp-matchall.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. // var Construct = require('es-abstract/2023/Construct');
  3. var CreateRegExpStringIterator = require('es-abstract/2023/CreateRegExpStringIterator');
  4. var Get = require('es-abstract/2023/Get');
  5. var Set = require('es-abstract/2023/Set');
  6. var SpeciesConstructor = require('es-abstract/2023/SpeciesConstructor');
  7. var ToLength = require('es-abstract/2023/ToLength');
  8. var ToString = require('es-abstract/2023/ToString');
  9. var Type = require('es-abstract/2023/Type');
  10. var flagsGetter = require('regexp.prototype.flags');
  11. var setFunctionName = require('set-function-name');
  12. var callBound = require('call-bind/callBound');
  13. var $indexOf = callBound('String.prototype.indexOf');
  14. var OrigRegExp = RegExp;
  15. var supportsConstructingWithFlags = 'flags' in RegExp.prototype;
  16. var constructRegexWithFlags = function constructRegex(C, R) {
  17. var matcher;
  18. // workaround for older engines that lack RegExp.prototype.flags
  19. var flags = 'flags' in R ? Get(R, 'flags') : ToString(flagsGetter(R));
  20. if (supportsConstructingWithFlags && typeof flags === 'string') {
  21. matcher = new C(R, flags);
  22. } else if (C === OrigRegExp) {
  23. // workaround for older engines that can not construct a RegExp with flags
  24. matcher = new C(R.source, flags);
  25. } else {
  26. matcher = new C(R, flags);
  27. }
  28. return { flags: flags, matcher: matcher };
  29. };
  30. var regexMatchAll = setFunctionName(function SymbolMatchAll(string) {
  31. var R = this;
  32. if (Type(R) !== 'Object') {
  33. throw new TypeError('"this" value must be an Object');
  34. }
  35. var S = ToString(string);
  36. var C = SpeciesConstructor(R, OrigRegExp);
  37. var tmp = constructRegexWithFlags(C, R);
  38. // var flags = ToString(Get(R, 'flags'));
  39. var flags = tmp.flags;
  40. // var matcher = Construct(C, [R, flags]);
  41. var matcher = tmp.matcher;
  42. var lastIndex = ToLength(Get(R, 'lastIndex'));
  43. Set(matcher, 'lastIndex', lastIndex, true);
  44. var global = $indexOf(flags, 'g') > -1;
  45. var fullUnicode = $indexOf(flags, 'u') > -1;
  46. return CreateRegExpStringIterator(matcher, S, global, fullUnicode);
  47. }, '[Symbol.matchAll]', true);
  48. module.exports = regexMatchAll;